1 - What is java collection framework?
The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures, it took shape with the release of JDK 1.2 and was expanded in JDK 1.4. It is a java data structure.
2 - What are the basic interfaces of Java Collections Framework?
The basic interfaces of java collection framework are:
- Collection - It declares the core methods that all collections will have.
- Set - HashSet, LinkedHashSet classes implements this interface as it declare common method.
- List - ArrayList, LinkedList, Vector classes implements this interface as it declare common method
- Map - Hashtable, LinkedHashMap classes implements this interface as it declare common method
- SortedSet - TreeSet Class implements this interface to pre-sort its elements.
- SortedMap - TreeMap class implements this interface to pre-sort its emenets.
3 - What are difference between ArrayList and Vector?
|Introduced in jdk 1.2 .
||Introduced in jdk first version.
|All methods are non-synchronized.
||All method are synchronized.
|Use index based insertion and searching.
||Use index based insertion and searching.
|Use dynamic resizing array internally as data structure.
||Use dynamic resizing array as data-structure internally.
|ArrayList increases by half of its size when its size is increased.
||Vector doubles the size of its array when its size is increased.
4 - What are difference between ArrayList and LinkedList?
ArrayList use array internally to insert and search the element whereas LinkedList use doubly LinkedList to insert and search the element. Random access is fast with arraylist as it find element by index, whereas LinkedList is slow as it traverse to find element.
ArrayList is fast and easy to use whereas LinkedList take more memory in comparison to ArrayList.
5 - What is difference between HashMap and HashSet?
|HashMap is a implementation of Map interface.
||HashSet is an implementation of Set Interface.
|HashMap Stores data in form of key and pair.
||HashSet Store data in form of objects.
|To add elements put() methods is used.
||To add element add() methods is used.
|In HashMap hashcode value is calculated using key object.
||Member object is used for calculating hashcode value. Two object may have same hash code.
|HashMap is faster than hashSet because unique key is used to access object.
||HashSet is slower than HashMap.
6 - What is difference between HashMap and Hashtable?
Both HashMap and Hashtable implements Map interface. They are differ in following ways
- HashMap allows null key and value whereas Hashtable doesn't allow nulls.
- HashMap is non synchronized whereas Hashtable is synchronized.
- HashMap use iterator whereas Hashtable use enumeration.
- HashMap is faster than Hashtable.
7 - What is difference between Enumeration and Iterator?
The major difference between enumeration and iterartor is that iterartor has a remove() method while enumeration doesn't have. Hence, enumerator behave as a read-only interface because it has a method to retrieve and traverse the object. Also Iterator is more secure and safe as compared to Enumeration because it doesn't allow other thread to modify the collection object while some thread is iterating over it and throws.
8 - When do you override hashcode and equals()?
equals() method is used to compare objects for equality while hashCode is used to generate an integer code corresponding to that object. To remove delicacy from user define objects from the the HashMap then you need to override equals() and hashCode() methods. As equals() method compare objects for equality and hashCode() method generate same hashcode for both object if they are equal.
9 - What is difference between Comparable and Comparator interface?
You need to use comparable and comparator when you want to sort user define objects.
- Use Comparable when you want only one sort sequence and use Comparator when you want many sort sequence.
- Comparable interface has only one method (int compareTo(Object o)) whereas Comparator interface has two methods (int compare (Object o1, Object o2); ,boolean equals(Object o); )
- Comparable lies in java.lang.Comparable package while Comparator lies in java.util.Comparator.
10 - What is Generic?
The Java Generics features were added to the Java language from Java 5. Now with generics, you can put only customer objects in the ArrayList, so the objects come out as customer references.
You no need to cast the object while retrieving but Before generics, there was no way to declare the type of an ArrayList, so its add() method took type Object.
11 - How can we make Hashmap synchronized?
You can make HashMap synchronized by using Collection class static method synchronizeMap.
Map m = Collections.synchronizeMap(hashMap);
12 - What is the difference between Sorting performance of Arrays.sort() and Collections.sort() ?
Collections.sort(List list) internally calls Arrays.sort(object a) method to do sorting. Arrays.sort(Object a) is for arrays so the sorting is done directly on the array. Arrays.sort() is faster than Collections.sort() because Collection.sort() method first elements into array and then pass it to Arrays.sort () whereas Arrays.sort ()the sorting is done directly on the array.
13 - What is difference between Array and ArrayList?
- Array is a fixed length data structure, you can,t change length of array once created whereas ArrayList is of dynamic data structure, it resize itself when gets full depending upon capacity and load factor.
- You canâ€™t use generics on array whereas ArrayList can be generics to ensure type-safety.
- Length keyword is use to calculate array length whereas ArrayList use size() method to get length.
- Array is capable to store both primitive as well as non-primitive while you canâ€™t store primitives in ArrayList.
14 - What is ConcurrentHashMap and When do you use ConcurrentHashMap in Java?
ConcurrentHashMap is introduced as an alternative to Hashtable in jdk 1.5 as part of java concurrency package. ConcurrentHashMap performs better than Hashtable and synchronizedMap because it only locks a portion of Map, instead of whole Map. Use ConcurrentHashMap can be safely used in concurrent multi-threaded environment but also provides better performance over Hashtable and synchronizedMap.
15 - What is toString()?
toString() is a Object class that returns the string representation of the object. If you print any object compiler invokes the Object class toString() method, So sub-classes overrides toString() for desire output.
16 - What are difference between Iterator and ListIterator?
|Use Iterator to traverse Set, List and also Map Objects.
||Use ListIterator to traverse list objects.
|Iterator iterate elements of collection in the forward direction only.
||ListIterator iterate elements of collection in the forward and backward direction.
|Iterator cannot add elements to collection.
||ListIterator can add elements to collection.
|Iterator can't replace existing object with new object.
||ListIterator can replace existing object with new object.
|Iterator can't get index of elements.
||ListIterator get index of elements list.
17 - Why do you get a ConcurrentModificationException when using an iterator?
Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException.
18 - What are Wrapper Classes and Why the wapper class is needed?
Wrapper classes are those which encapsulate a primitive type within an object. Wrapper classes are Byte, Short, Integer, Long, Character, Boolean, Double, Float, they offers methods that allow to fully integrate with the primitive types into Javaâ€™s object.
We need wrapper classes as sometimes it is easier to deal with primitives as objects. Most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these reasons we need wrapper classes. We can also make objects of these classes to store in collection.
19 - How HashMap works in Java?
- HashMap works on hashing priciple.
- put(key, value) and get(key) methods are there for storing and retrieving Objects from HashMap.
- . When you pass Key and Value to put() method , HashMap implementation calls hashCode method on Key object and applies returned hashcode into its own hashing function to find a bucket location for storing Entry object , HashMap in stores both key and value object as Map.
20 - What is the Properties class?
- Properties is a subclass of Hashtable.
- It is used to maintain lists of values in which the key is a String and the value is also a String.
- The Properties class is used by many other Java classes.
- For example, to get the system properties you use System.getProperties( ) that gives you properties like os, jdk version etc.