Last Updated on July 9, 2023 by KnownSense
31. What are Vectors in java?
The vector is the same as an array. It has components that can be accessed using an index value. Vectors can contain a legacy method that is nor part of the collection framework.
32. What is dictionary class in java?
The Dictionary class is a Java class that has a capability to store key-value pairs.
33. What is an EnumSet?
java.util.EnumSet is Set implementation that can be used with enum types. EnumSet having all elements must come from one enum type specified explicitly or implicitly. It is not synchronized, and also null keys are not allowed. EnumSet provides methods like EnumSetof(E first, E… rest), complementOf(EnumSet s), and copyOf(Collection c).
34. What are the ways to remove duplicate from ArrayList?
Two ways to remove duplicates from ArrayList are:
- HashSet: Developer can use HashSet to remove the duplicate element from the ArrayList. The drawback is it cannot preserve the insertion order.
- LinkedHashSet: Developers can also maintain the order of insertion by using LinkedHashSet instead of HashSet.
35. What is IdentityHashMap?
IdentityHashMap is a class that implements Serializable, Clonable interfaces, Map, and extends AbstractMap class. It is designed for the case wherein there is a need of reference-equality semantics.
36. How can we make collection thread-safe?
The methods to make collection thread safe are:
– Collections.synchronizedList(list);
– Collections.synchronizedMap(map);
– Collections.synchronizedSet(set);
37. Difference between Queue and DeQueue?
Queue | Deque |
It is called a single-ended Queue | It is called a double-ended Queue |
Elements in the Queue are added or removed from one end | Elements in the Queue are added from either end can be added and removed from the both end |
It is less versatile. | It is more versatile. |
38. What is peek() of Queue interface?
Peek () is a method of queue interface. It retrieves all the elements but does not remove the queue head. In case if the Queue is empty, then this method will return null.
39. What is CopyOnWriteArrayList?
CopyOnWriteArrayList is a variant of ArrayList in which operations like add and set are implemented by creating a copy of the array. It is a thread-safe, and thereby it does not throw ConcurrentModificationException. This ArrayLists permits all the elements, including null.
40. Explain iterator interface methods?
Method | Description |
public boolean hasNext() | It returns true in the iterator has elements; otherwise, it returns false. |
public Object next() | This method returns the element and moves the pointer to the next value. |
public void remove() | This Java method can remove the last elements returned by the iterator. Public void remove() is less used. |
Authored by codingknownsense.com