Java.Collections

What is a “collection”?

Name the main JCF interfaces and their implementations.

Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap,

Why Do Keys with Different Hash Codes End Up in the Same Bucket in a HashMap?

What is SortedSet ?

What is LinkedHashSet ?

Why do i need insertion order and what is difference to sortedOrder ?

Arrange the following interfaces in a hierarchy: List, Set, Map, SortedSet, SortedMap, Collection, Iterable, Iterator, NavigableSet, NavigableMap.

What is the difference between java.util.Collection and java.util.Collections?

What is “fail-fast behavior”?

Delve in fail fast in collections

Why can’t we modify a collection while iterating over it?

What is the difference between fail-fast and fail-safe?

What is the difference between Enumeration and Iterator.

How are Iterable and Iterator related?

Iterator, and “for-each” related?

Compare Iterator and ListIterator.

What about fail fast in list iterator

What happens if you call Iterator.next() without first calling Iterator.hasNext()?

How many elements will be skipped if Iterator.next() is called after 10 calls to Iterator.hasNext()?

How will collection behave if iterator.remove() is called?

How will an instantiated iterator for collection behave if collection.remove() is called?

How to avoid ConcurrentModificationException when iterating over a collection?

Which collection implements the FIFO maintenance discipline?

Which collection implements the FILO maintenance discipline?

Why ArrayDeque better than Stack ?

What is the difference between ArrayList and Vector?

Why was ArrayList added if Vector already existed?

What is the difference between ArrayList and LinkedList? When is it better to use the former and when is it better to use the latter?

What is faster ArrayList or LinkedList?

What is the worst case running time of the contains() method for an element that is in a LinkedList?

What is the worst case running time of the contains() method for an element that is in an ArrayList?

What is the worst case running time of the add() method for a LinkedList?

What is the worst case execution time of the add() method for ArrayList?

You need to add 1 million elements, what structure do you use?

How are elements removed from an ArrayList? How does the size of the ArrayList change in this case?

Suggest an efficient algorithm for removing several adjacent elements from the middle of a list implemented by ArrayList.

How much additional memory is needed when calling ArrayList.add()?

How much additional memory is allocated when calling LinkedList.add()?

What is Object header ?

Estimate the amount of memory required to store one byte primitive in LinkedList?

What is JVM Padding ?

Estimate the amount of memory required to store one byte primitive in ArrayList?

Is the operation of adding an element to the middle (list.add(list.size()/2, newElement)) slower for ArrayList or for LinkedList?

The implementation of the ArrayList class has the following fields: Object[] elementData, int size. Explain why it is necessary to store size separately if you can always take elementData.length?

Compare the interfaces of Queue and Deque

Who extends whom: Queue extends Deque, or Deque extends Queue?

Why does LinkedList implement both List and Deque?

Is LinkedList singly linked, doubly linked, or quadruple linked?

How to iterate through LinkedList elements in reverse order without using slow get(index)?

What can PriorityQueue do?

Stack is considered “obsolete”. What is recommended to replace it? Why?

Why do we need HashMap if we have Hashtable?

What is the difference between HashMap and IdentityHashMap? What is IdentityHashMap used for?

What is the difference between HashMap and WeakHashMap? What is WeakHashMap used for?

What is weak reference ?

Strong references, weak, soft, phantom

WeakHashMap uses WeakReferences. Why not create a SoftHashMap on SoftReferences?

WeakHashMap uses WeakReferences. Why not create a PhantomHashMap on PhantomReferences?

What is WeakHashMap?

LinkedHashMap – what is from LinkedList and what is from HashMap?

How does SortedMap “sort” itself, other than the fact that toString() outputs all elements in order?

How is HashMap constructed?

According to Knuth and Cormen, there are two main implementations of a hash table: open addressing and chaining. How is HashMap implemented? Why do you think this implementation was chosen? What are the pros and cons of each approach?

How does HashMap work when trying to store two elements in it by keys with the same hashCode(), but for which equals() == false?

What is the initial number of buckets in HashMap?

What is the estimated time complexity of operations on elements from HashMap? Does HashMap guarantee the specified complexity of element selection?

Is it possible for HashMap to degenerate into a list even with keys having different hashCode()?

In what case can an element be lost in HashMap?

Why can’t I use byte[] as a key in a HashMap?

What is the role of equals() and hashCode() in a HashMap?

What is the maximum number of hashCode() values?

What is the worst case runtime of get(key) for a key that is not in the HashMap?

What is the worst case runtime of get(key) for a key that is in the HashMap?

Why does a doubly linked list always convert to a red-black tree even though the key in a HashMap does not have to implement the Comparable interface?

How many transitions are involved when calling HashMap.get(key) on a key that is in the table?

How many new objects are created when you add a new element to a HashMap?

Explain the meaning of the parameters in the HashMap(int initialCapacity, float loadFactor) constructor.

Will HashMap work if all added keys have the same hashCode()?

How to iterate over all keys in a Map?

How to iterate over all values ​​in a Map?

How to iterate over all key-value pairs in a Map?

What is the difference between TreeSet and HashSet?

What happens if you add elements to a TreeSet in ascending order?

How does LinkedHashSet differ from HashSet?

There is a special class for Enum. java.util.EnumSet. Why? Why were the authors not satisfied with HashSet or TreeSet?

What are the ways to iterate over list elements?

How can you get synchronized objects of standard collections?

How can you get a read-only collection?

Write a single-threaded program that causes a collection to throw a ConcurrentModificationException.

Give an example of a collection throwing an UnsupportedOperationException.

Implement a symmetric difference of two collections using Collection methods (addAll(…), removeAll(…), retainAll(…)).

How to make a cache with “invalidation policy” using LinkedHashMap?

How to copy elements of any collection to an array in one line?

How to get a List with all elements except the first and last 3 in one call from List?

How to convert a HashSet to an ArrayList in one line?

How to convert an ArrayList to a HashSet in one line?

If you want to create a HashSet from the keys of a HashMap, it’s super simple — just grab the key set and pass it to the HashSet constructor.

Make a HashMap from a HashSet<Map.Entry<K, V>>.