76. What is Enumeration interface?
If "hasMoreElements()" returns true and we want to fetch the next object in the collection, use nextElement() method. In case there is not more objects exists in the collection and if we call nextElement() method, the runtime NoSuchElementException will be thrown.
- Iterators allow the programmer to remove elements of a collection during the iteration. Method names used by the iterator are shorter compared to Enumeration interface.
- Method hasNext() of the Iterator interface returns true, if the iteration has more elements. To get the next object in the iteration, call next() Method. To remove the last element returned by the iterator, invoke remove() method which is optional.
78. What is SortedSet interface?
SortedSet extends Set interface, which make sure that the elements of a set is sorted. An example of a SortedSet interface is TreeSet. We can use iterator to fetch the sorted elements in the set.
79. How elements can be sorted in java collections?
The elements can be sorted in two ways. One way is to use the Comparable interface. Objects can be compared using the compareTo() method. This approach is referred to as a natural order.
The other way is to specify a comparator for the elements in the set if the class doesn't implement the Comparable interface, or you don’t want to use the compareTo() method in the class that implements the Comparable interface. This approach is referred to as order by comparator
80. What is a list interface?
- The List interface allows us to create a list of elements. The List interface extends the Collection interface.
- A list allows us to store duplicate elements in a collection. A list also allows to specify where the element is to be stored. The user can access the element by index.
81. Compare ArrayList vs LinkedList?
- ArrayList uses dynamic array to store the data, whereas the LinkedList uses a doubly linked list.
- Data manipulation is faster with LinkedList compared to the ArrayList.
- ArrayList can act only as a list, whereas the LinkedList can act as a list and a queue.
82. What is a vector class?
The Vector class is similar to ArrayList, when it comes to accessing the elements in a collection. Once an object of the vector is created, it's size can grow or shrink when we add or remove items in a Vector. It is synchronized by default.
83. What is a Stack class?
The Stack class represents a last-in-first-out (LIFO) stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack.
84. What is a Map interface?
A Map is a storage that maps keys to values. Map doesn't support duplicate keys and each key maps to at most one value. The Map interface is not an extension of the Collection interface. Instead the interface starts off it's own interface hierarchy. The interface describes a mapping from keys to values, without duplicate keys. The keys are like indexes. In List, the indexes are integer. In Map, the keys can be any object.
85. Describe about HashMap and TreeMap?
The HashMap and TreeMap classes are two concrete implementations of the Map interface. Both will require key &value. Keys must be unique. The HashMap class is efficient for locating a value, inserting a mapping, and deleting a mapping. The TreeMap class, implementing SortedMap, is efficient for traversing the keys in a sorted order.
HashMap allows null as both keys and values. When it comes to speed, TreeMap is slower than HashMap. In TreeMap, an optional Comparator object is used to decide the order in which the keys are sorted.
86. What is the purpose of Collections class in util package?
The Collections class provides various methods to operate on collections and maps, synchronizing collection classes and making collection classes read-only.
87. How do we order/sort objects in collections?
To provide ordering/sorting of the objects, Java API provides two interfaces Comparable and Comparator
- Comparable interface is in java.lang
- Comparator interface is in java.util
88. When to use Comparable and Comparator in code?
The Comparable interface is simpler and less work
- Your class implements Comparable
- You provide a public int compareTo(Object o) method
- No argument is passed to your TreeSet or TreeMap constructor
- You will use the same comparison method every time
The Comparator interface is more flexible but slightly more work
- We can create classes that implement Comparator as we wish
- You can sort the TreeSet or TreeMap differently with each Comparator
- TreeSet or TreeMap use the comparator in constructor as required. For example, sort Students by score or by name
Suppose you have students sorted by score, in a TreeSet you call studentsByScore. Now you want to sort them again, this time by name
Comparator<Student> myStudentNameComparator = new MyStudentNameComparator();
TreeSet studentsByName = new TreeSet(myStudentNameComparator);