Java Collection Interview Questions

Java Collection Interview Questions


1).What is Collection framework?

ans:collection is nothing but the group objects represents as  a single unit is known as collection

2).Already we have arrays why we are using Collection framework?
 ans:
  Arrays are fixed size,once we declare  Array size we can't change the size of the Array
 so we are using collection
     

3).Explain Collections and Collection words?
  ans:
      collections:(class)
           collection class provide several utility methods for collection object,like......soarting,searching etc.....

      collection:(interface)
           collection is nothing but the group objects represents as  a single unit is known as collection
       

4).Explain Collection hierarchy?

5).Explain List,Map,Set.
  ans:
       List:
             * list is the child interface of collection
             * list is nothing but the group objects represents as  a single unit where duplicats are allowed
                and insertation must be preserved then we should go for list
       Map:

           *if we want represent a group of objects as a key value pairs then we should go for map
       set:
         
             * set is child interface of collection
             *  set is nothing but the group objects represents as  a single unit where duplicats are not allowed
                and insertation  not requied then we should go for list
     

6).How to retrieve the elements in collection objects?How many ways to retrieve the collection objects?

7).What is the difference b/w ArrayList and Vector ?

  ans:
     
ArrayList                                                                                              Vector
1) ArrayList is not synchronized.                                         Vector is synchronized.
2) ArrayList increments 50% of current array size if number of
            element exceeds from its capacity.                                   Vector increments 100% means doubles the array size if total number
                                                                                         of element exceeds than its capacity.
3) ArrayList is not a legacy class, it is introduced in JDK 1.2.          Vector is a legacy class.
4) ArrayList is fast because it is non-synchronized.                         Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
5) ArrayList uses Iterator interface to traverse the elements.                   Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
           *
8).When we are using the ArrayList and Vector?
  ans:
    ArrayList uses Iterator interface to traverse the elements.
   
     Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
         
9).What is the difference b/w ArrayList and LinkedList in your project?

ans:
        ArrayList                                                                       LinkedList
1) ArrayList internally uses dynamic array to store the elements.            LinkedList internally uses doubly linked list to store the elements.
2) Manipulation with ArrayList is slow because it internally uses array.
  If any element is removed from the array, all the bits are shifted in memory      Manipulation with LinkedList is faster than ArrayList because
                                                                                   it uses doubly linked list so no bit shifting is required in memory.
3) ArrayList class can act as a list only because it implements List only.   LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data.                           LinkedList is better for manipulating data.

10).When you are using the ArrayList and LinkedList in your project?

ans:
      LinkedList is used for manipulating data.

     ArrayList is used for storing and accessing data.

11).Can you create your own ArrayList?


12).What is the difference b/w Hashtable and HashMap?
   
ans:
   HashMap allows one null key and multiple null values.        Hashtable doesn't allow any null key or value.
3) HashMap is a new class introduced in JDK 1.2.                Hashtable is a legacy class.
4) HashMap is fast.                                                Hashtable is slow.
5) We can make the HashMap as synchronized by calling this code
   Map m = Collections.synchronizedMap(hashMap);                  Hashtable is internally synchronized and can't be unsynchronized.
6) HashMap is traversed by Iterator.                                  Hashtable is traversed by Enumerator and Iterator.
7) Iterator in HashMap is fail-fast.                                  Enumerator in Hashtable is not fail-fast.
8) HashMap inherits AbstractMap class.                                   Hashtable inherits Dictionary class.

13).When you are using the Hashtable and HashMap in your project?



14).Exaplain HashMap internal workflow?(How HashMap works internally).


15).How HashSet works internally?


16).What is the difference b/w HashSet and LinkedHashSet?

ans :
      Hashset:
       uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
        contains unique elements only.

         LinkedHash set:
             contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
             maintains insertion order.
 
       

17).Difference b/w HashSet and TreeSet?
ans:
      Hashset:
       uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
        contains unique elements only.
 
        Treeset:
        contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
         maintains ascending order.


18).When you are using the HashSet and LinkedHashSet in your project?




19).What is the o/p of the below program?


  public class Test {
  public static void main(String args[]) {
    HashSet<Integer> hs = new HashSet<Integer>
    hs.add(1);
    hs.add(1);
    hs.add(1);  
    hs.add(1);
    System.out.println(hs);
  }
}
20).What is the o/p of the below program?

  public class Test {
  public static void main(String args[]) {
    HashMap<Integer,String> hs = new HashMap<Integer,String>
    hs.put(1,"jana");
    hs.put(1,"Krish");
    hs.put(1,"jagan");  
    hs.put(1,"nag");
    System.out.println(hs);
  }
}
21).What is the difference b/w Iterator and ListIterator?

     ans:
        iteretor can travrse the elements in forword direction only

     ListIterator can travrse the elements in forword and backword direction

22).What is the difference b/w Enumeration and Iterator?

  ans:
        iteretor can travrse the elements in forword direction only

    Enumeration:
         This is legacy interface and defines the methods by which you can enumerate (obtain one at a time)
         the elements in a collection of objects. This legacy interface has been superceded by Iterator.



23).What is the difference b/w HashMap and ConcurrentHashMap?

ans:
 
24).Difference b/w WeakHashMap and IdentityHashMap?


25).What is the contract b/w equals() and hashCode() methods?


26).When exactly you are overriding the equals() method and hashCode() methods?


27).How to retrieve the HashMap elements.Write a sample program(In Collection API we have 3 ways like entrySet(),keySet() and values() methods)


28).Difference b/w HashMap and TreeMap?

ans:TreeMap:
     A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
     It contains only unique elements.
     It cannot have null key but can have multiple null values.
     It is same as HashMap instead maintains ascending order.

    HashMap:
      A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
      It contains only unique elements.
      It may have one null key and multiple null values.
      It maintains no order.



29).What is the difference b/w Comparator and Comparable interfaces?

    ans:
 1) Comparable provides single sorting sequence. In other words, we can sort the
    collection on the basis of single element such as id or name or price etc.        Comparator provides multiple sorting sequence. In other words,
                                                                                        we can sort the collection on the basis of multiple elements such as id, name and price etc.
2) Comparable affects the original class i.e. actual class is modified.                Comparator doesn't affect the original class i.e. actual class is not modified.
3) Comparable provides compareTo() method to sort elements.                        Comparator provides compare() method to sort elements.
4) Comparable is found in java.lang package.                                        Comparator is found in java.util package.
5) We can sort the list elements of Comparable type by Collections.sort(List) method. We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

30).Why we are using the Comparator and Comparable interfaces?Write a sample program.


31).Explain enhanced for loop.

ans:
    The for-each loop is used to traverse array or collection in java. It is easier to use than simple for
     loop because we don't need to increment value and use subscript notation.

         It works on elements basis not index. It returns element one by one in the defined variable.

32).Difference b/w Iterator and for-each?
ans:
       The for-each loop is used to traverse array or collection in java. It is easier to use than simple for
     loop because we don't need to increment value and use subscript notation.

         It works on elements basis not index. It returns element one by one in the defined variable.
iterator:
 it can travarse the elements in forword dirction


33).List  Vs Set?
    list can allowd duplicat elements
     set can't allowdduplicate elements

34).HashMap vs IdentityHashMap?


35).HashMap vs WeakHashMap?


36).Stack vs Queue?

ans:
  Queue;
     The Queue interface basically orders the element in FIFO(First In First Out)manner.
  Stack:
        Stack is a subclass of Vector that implements a standard last-in, first-out stack.

      Stack only defines the default constructor, which creates an empty stack.
        Stack includes all the methods defined by Vector, and adds several of its own.

37).ArrayList vs CopyOnWriteArrayList?


38).Explain Properties class.




39).How to create the Synchronized ArrayList?



40).What is the use of generics ? Why we are using Generics?Explain



41).What are the key interface in Collection framewore?(9 key interfaces are available)(Explain each one)

       Collection
       List
       Set
       SortedSet
       NavigableSet
       Queue
       Map
       SortedMap
       NavigableMap


42).Set vs Map?
ans:
       Map:

           *if we want represent a group of objects as a key value pairs then we should go for map
       set:
         
             * set is child interface of collection
             *  set is nothing but the group objects represents as  a single unit where duplicats are not allowed
                and insertation  not requied then we should go for list
     

43).How to create synchronized HashMap?