Java Collections Framework Interview Questions
1) The difference between List, Set, Map, and Queue in Java? (answer)
The list is an ordered collection which allows duplicate. It also has an implementation which provides constant time index based access, but that is not guaranteed by List interface. Set is unordered collection which
49) Difference between poll() and remove() method?
Both poll() and remove() take out the object from the Queue but if poll() fails then it returns null but if remove fails it throws Exception.
2) The difference between LinkedHashMap and PriorityQueue in Java? (answer)
PriorityQueue guarantees that lowest or highest priority element always remain at the head of the queue, but LinkedHashMap maintains the order on which elements are inserted. When you iterate over a PriorityQueue, iterator doesn’t guarantee any order but iterator of LinkedHashMap does guarantee the order on which elements are inserted.
3) Difference between ArrayList and LinkedList in Java? (answer)
The obvious difference between them is that ArrrayList is backed by array data structure, supprots random access and LinkedList is backed by linked list data structure and doesn’t supprot random access. Accessing an element with the index is O(1) in ArrayList but its O(n) in LinkedList. See the answer for more detailed discussion.
4) What is a couple of ways that you could sort a collection? (answer)
You can either use the Sorted collection like TreeSet or TreeMap or you can sort using the ordered collection like a list and using Collections.sort() method.
5) How do you print Array in Java? (answer)
You can print an array by using the Arrays.toString() and Arrays.deepToString() method. Since array doesn’t implement toString() by itself, just passing an array to System.out.println() will not print its contents but Arrays.toString() will print each element.
6) LinkedList in Java is doubly or singly linked list? (answer)
It’s a doubly linked list, you can check the code in JDK. In Eclipse, you can use the shortcut, Ctrl + T to directly open this class in Editor.
55) Which kind of tree is used to implement TreeMap in Java? (answer)
A Red Black tree is used to implement TreeMap in Java.
7) What is the difference between Hashtable and HashMap? (answer)
There are many differences between these two classes, some of them are following:
a) Hashtable is a legacy class and present from JDK 1, HashMap was added later.
b) Hashtable is synchronized and slower but HashMap is not synchronized and faster.
c) Hashtable doesn’t allow null keys but HashMap allows one null key.
See the answer for more differences between HashMap and Hashtable in Java.
8) How HashSet works internally in Java? (answer)
HashSet is internally implemented using an HashMap. Since a Map needs key and value, a default value is used for all keys. Similar to HashMap, HashSet doesn’t allow duplicate keys and only one null key, I mean you can only store one null object in HashSet.
9) Write code to remove elements from ArrayList while iterating? (answer)
Key here is to check whether candidate uses ArrayList’s remove() or Iterator’s remove(). Here is the sample code which uses right way o remove elements from ArrayList while looping over and avoids ConcurrentModificationException.
10) Can I write my own container class and use it in the for-each loop?
Yes, you can write your own container class. You need to implement the Iterable interface if you want to loop over advanced for loop in Java, though. If you implement Collection then you by default get that property.
11) What is default size of ArrayList and HashMap in Java? (answer)
As of Java 7 now, default size of ArrayList is 10 and default capacity of HashMap is 16, it must be power of 2. Here is code snippet from ArrayList and HashMap class :
// from ArrayList.java JDK 1.7
private static final int DEFAULT_CAPACITY = 10;
//from HashMap.java JDK 7
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
12) Is it possible for two unequal objects to have the same hashcode?
Yes, two unequal objects can have same hashcode that’s why collision happen in a hashmap.
the equal hashcode contract only says that two equal objects must have the same hashcode it doesn’t say anything about the unequal object.
13) Can two equal object have the different hash code?
No, thats not possible according to hash code contract.
14) Can we use random numbers in the hashcode() method? (answer)
No, because hashcode of an object should be always same. See the answer to learning more about things to remember while overriding hashCode() method in Java.
15) What is the difference between Comparator and Comparable in Java? (answer)
The Comparable interface is used to define the natural order of object while Comparator is used to define custom order. Comparable can be always one, but we can have multiple comparators to define customized order for objects.
16) Why you need to override hashcode, when you override equals in Java? (answer)
Because equals have code contract mandates to override equals and hashcode together .since many container class like HashMap or HashSet depends on hashcode and equals contract.