Collections Section Index
What is a Deque?
A Deque is a double ended queue, allowing inserting and removing from both ends.
What purpose does the newSetFromMap() method offer if I can just create something like a HashSet instead of a HashMap?
The Set returned by newSetFromMap() functions with the same ordering and concurrency characteristics of the map it is based off of. Some Map implementations also have a related Set implement, so u...more
Why should all Comparators be Serializable?
If you pass a Comparator that doesn't implement Serializable into a collection like a TreeMap, that collection will automatically be non-serializable. It is best to flag all Comparator implementat...more
How can I detect if a duplicate is added to a Set?
The add() method returns a boolean. If the set did not previously contain the element, true is returned. If the set previoulsy did contain the element, false is returned. Of course, you can also c...more
How do I create a multimap in Java?
Typically a Map maps a key to a single value. To have a multimap, the values of a Map should be a List instead. Thus there can be multiple values in the List associated with a single key.more
Is it better to use a for-each loop or an Iterator with a collection?
It depends on what you need to do. The remove() method of an Iterator is the safe way to remove elements from an underlying collection. You can't safely remove elements with a for-each loop. For j...more
What does Class<String> mean?
Class<String> is the generics way of writing String.class. It allows you to improve type safety.
What does Class<String> mean?
Class<String> is the generics way of writing String.class. It allows you to improve type safety.
What is a multimap?
A multimap is a map where a single key can map to multiple values.
What is a multimap?
A multimap is a map where a single key can map to multiple values. (And a value can be associated with multiple keys.)
What is the natural ordering of a Boolean? Does FALSE come first or second?
Boolean.FALSE < Boolean.TRUE
Why do I keep getting an UnsupportedOperationException thrown when I try some collection operations?
Several methods of the collection interfaces are considered optional. If an unsupported operation is called, that is when you'll see the UnsupportedOperationException thrown. The javadoc for a col...more
How do you pronounce Deque?
Deque is pronounced like deck, not de-queue.
What implementations of the Deque interface does Java offer?
ArrayDeque offers an array backed deque. LinkedList offers a linked list version. LinkedBlockingDeque offers an optionally bounded blocking version.
What is the purpose of the CopyOnWriteArrayList and CopyOnWriteArraySet collections?
Synchronized operations are costly and if you aren't modifying a collection much, it is best not to synchronize all collection accesses. The CopyOnWriteXXX collections avoid concurrent modificatio...more