Java Collections Interview Questions57.What is the Set interface ?
58.What are the main Implementations of the Set interface ?
59.What is a HashSet ?
60.What is a TreeSet ?TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time. 61.What is an EnumSet ?An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created 62.Difference between HashSet and TreeSet ?
63. What is a Map ?
64. What are the main Implementations of the Map interface ?The main implementations of the Map interface are as follows:
65.What is a TreeMap.?TreeMap actually implements the SortedMap interface which extends the Map interface. In a TreeMap the data will be sorted in ascending order of keys according to the natural order for the key's class, or by the comparator provided at creation time. TreeMap is based on the Red-Black tree data structure. 66.How do you decide when to use HashMap and when to use TreeMap ?For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative. If, however, you need to traverse the keys in a sorted order, then TreeMap is your better alternative. Depending upon the size of your collection, it may be faster to add elements to a HashMap, then convert the map to a TreeMap for sorted key traversal. 67. Difference between HashMap and Hashtable ?
68.How does a Hashtable internally maintain the key-value pairs?TreeMap actually implements the SortedMap interface which extends the Map interface. In a TreeMap the data will be sorted in ascending order of keys according to the natural order for the key's class, or by the comparator provided at creation time. TreeMap is based on the Red-Black tree data structure. 69. What Are the different Collection Views That Maps Provide?Maps Provide Three Collection Views.
70. What is a KeySet View ?KeySet is a set returned by the keySet() method of the Map Interface, It is a set that contains all the keys present in the Map. 71. What is a Values Collection View ?Values Collection View is a collection returned by the values() method of the Map Interface, It contains all the objects present as values in the map. 72. What is an EntrySet View ?Entry Set view is a set that is returned by the entrySet() method in the map and contains Objects of type Map. Entry each of which has both Key and Value. 73.How do you sort an ArrayList (or any list) of user-defined objects ?Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, Comparator). 74. What is the Comparable interface ?The Comparable interface is used to sort collections and arrays of objects using the
interface Comparable<T> where T is the name of the type parameter. int i = object1.compareTo(object2)
75. What are the differences between the Comparable and Comparator interfaces ?
|