Top Collection Java Interview Questions

Q) Which data structure is used to implement HashSet?

Ans) HashSet implements Hashmap internally to store the data. The data passed to hashset is stored as a key in hashmap with null as value. This can be summarized as HashMap with keys and null value is a hashset.

Q) What is a ConcurrentHashMap?

Ans) A concurrentHashMap is a thread-safe implementation of Map interface. In this class put and remove methods are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance. The allowed concurrency among update operations is guided by the optional concurrencyLevel constructor argument (default 16), which is used as a hint for internal sizing. The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, the actual concurrency will vary. Ideally, you should choose a value to accommodate as many threads as will ever concurrently modify the table. Using a significantly higher value than you need can waste space and time, and a significantly lower value can lead to thread contention. But overestimates and underestimates within an order of magnitude do not usually have much noticeable impact. A value of one is appropriate when it is known that only one thread will modify and all others will only read. Also, resizing this or any other kind of hash table is a relatively slow operation, so, when possible, it is a good idea to provide estimates of expected table sizes in constructors.

Q) What is the difference between iterator access and index access??

Ans) Index based access allows access of the element directly on the basis of index. The cursor of the data structure can directly goto the 'nth' location and get the element. It does not have to traverse through n-1 elements.

In Iterator based access, the cursor has to traverse through each element to get the desired element. So to reach the 'nth' element it needs to traverse through n-1 elements.

Insertion, updation or deletion will be faster for iterator based access if the operations are performed on elements present in middle of the data structure.

Insertion, updation or deletion will be faster for index based access if the operations are performed on elements present at the end of the data structure.

Traversal or search in index based data structure is faster.

ArrayList is index access and LinkedList is iterator access.

Q) How to sort a list in reverse order?

Ans) To sort the elements in the reverse natural order of the strings, get a reverse Comparator from the Collections class with reverseOrder(). Then, pass the reverse Comparator to the sort() method.

List list = new ArrayList();
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp)

Q) Can a null element be added to a Treeset or HashSet?

Ans) A null element can be added only if the set is of size 1 because when a second element is added then as per set definition a check is made to check duplicate value and comparison with null element will throw NullPointerException.
HashSet is based on hashMap and can contain null element.

Q) How to sort a list of strings - case insensitive ?

Ans) Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

Q) How to make a List (ArrayList,Vector,LinkedList) read only ?

Ans) A list implemenation can be made read only using Collections.unmodifiableList(list). This method returns a new list. If a user tries to perform add operation on the new list; UnSupportedOperationException is thrown.

Q) Which is faster to iterate LinkedHashSet or LinkedList?

Ans) LinkedList.

Q) Arrange in the order of speed - HashMap, HashTable, Collections.synchronizedMap, ConcurrentHashmap.

Ans) HashMap is fastest, ConcurrentHashMap, Collections.synchronizedMap, HashTable.

Q) What is WeakHashMap?

Ans) A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

Subscribe to get latest updates.

© 2015 Java Questions | Sitemap