Collections Flashcards
What are legacy collections?
Collections which were provided with JDK 1.0 have come to be known as legacy collections.
Examples include Hashtable and Vector. These are thread-safe and utilize synchronization to be so. Synchronization is expensive and can slow down the overall execution of a program.
What is the Collections Framework introduced in JDK 1.2?
The Collections Framework was introduced with JDK 1.2 and provided several utility collections.
The Collections Framework in JDK 1.2 avoided thread-safe collections as not all use cases require thread-safety. Synchronization code required to make collections thread-safe can also slow down performance and is unneeded in single threaded scenarios. The collections included in this framework are the ones you are most likely familiar with such as HashMap, LinkedList, ArrayList etc.
[1] List interfaces included in the Collection framework and classes that implement that collection.
educative.io/courses/java-interview-handbook/3jwW6ZmPXqx
INTERFACES
[1] Iterable, Collection
[2] LIST - ArrayLink, LinkedList, Vector
[3] MAP - HashMap, TreeMap, LinkedListMap
[4] SET - HashSet, LinkedHashSet
[5] QUEUE - PriorityQueue
[6] DEQUEU - ArrayDeqeue
[7] SORTED_SET - TreeSet
Class Stack extends Vector
What are wrapped collections?
https://www.educative.io/courses/java-interview-handbook/3jwW6ZmPXqx
When the Collections Framework was introduced in JDK 1.2, it didn’t come with collections that were synchronized. However, to later for multithreaded scenarios, the framework provided static methods to wrap vanilla collections in thread-safe wrapper objects. These thread-safe wrapper objects came to be known as wrapper collections.
Example Wrapper Collection
ArrayList<Integer> myList = new ArrayList<>();
List<Integer> syncList = Collections.synchronizedList(myList);</Integer></Integer>
For design pattern fans, this is an example of the decorator pattern.
List the collection interfaces in Java and describe them?
Following are the collection interfaces in java.util which are implemented by various classes. The core collection interfaces are the foundation of the Java Collections Framework.
Set A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
List A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
Queue Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator. Whatever the ordering used, elements of the queue are always removed from the head of the queue.
Dequeue The name deque is short for “double ended queue” and is usually pronounced “deck”. This interface defines methods to access the elements at both ends of the deque. A deque can be used both as a LIFO and FIFO structure.
Map An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. It replaces the abstract class Dictionary (now obsolete) which was part of the legacy collections.
How is it possible to get ConcurrentModificationException exception from single threaded code?
https://www.educative.io/courses/java-interview-handbook/7Xvmqp7r1DA
If a collection is being iterated upon by a thread but is also mutated at the same time by another thread, the ConcurrentModificationException exception is thrown. Iterators exhibiting this behavior are called fail-fast iterators as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future. The general-purpose Collections Framework iterators are fail-fast.
In case of a single thread a sequence of method invocations that violates the contract of an object will result in this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. An example is given below:
import java.util.*;
class Demonstration {
public static void main( String args[] ) {
ArrayList<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
myList.add(3);</Integer>
Iterator<Integer> it = myList.iterator(); while (it.hasNext()) { int item = it.next(); if (item % 2 == 0) { // modify the state of the underlying collection myList.add(5); } } } }
However, we can still use remove() method from the Iterator interface to remove the current element the iterator is pointing at. The above example is modified below to remove all the elements from the list.
import java.util.*;
class Demonstration {
public static void main( String args[] ) {
ArrayList<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
myList.add(3);</Integer>
Iterator<Integer> it = myList.iterator(); while (it.hasNext()) { int item = it.next(); it.remove(); } System.out.println(myList.size()); } }
What is the difference between a LinkedHashSet and a HashSet?
The difference between the two classes is the order in which the elements of the two collections can be iterated upon.
LinkedHashSet uses a linked list to maintain the order of insertion of key, value pairs and an iterator of the class can iterate over the (key, value) pairs in their inserted order. On the contrary, the HashSet’s iterator will iterate over the inserted (key, value) pairs in an arbitrary order.
What is the difference between Hashtable and HashMap?
The differences are:
a) Hashtable is thread-safe and predates HashMap. On the other hand HashMap promises no thread safety.
b) Hashtable doesn’t allow null keys, whereas HashMap allows a single null key.
c) HashMaphas a subclass LinkedHashMap which can be used to iterate over entries in the insertion order, whereas there’s no such facility for the Hashtable.
d) Hashtable is supported but considered obsolete. If a thread-safe version of the HashMap is required, we can always use static methods to get synchronized wrappers using Collections.synchronizedMap() or use ConcurrentHashMap .