Java Collections

java

https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html

Set
HashSet implements Set
SortedSet
LinkedHashSet implements Set
    is ordered - iterates in order of insertion
NavigatableSet extends SortedSet
TreeSet implements Set, NavigatableSet
    does not maintain the order of insertion, but it stores the elements in the sorted order.

List
ArrayList
LinkedList
Vector

Map
HashMap
    allows a key with null value.  Hashtable doesn't.
    HashMap is not thread-safe
Hashtable
    is a synchronized, thread-safe version of HashMap
TreeMap
    sorted order is maintained
LinkedHashMap
    insertion order is maintained, slower insertion and deletion, faster iteration
SortedMap - orders based on the keys.
NavigatableMap

Queue
BlockingQueue
PriorityQueue

What are the differences between ArrayList and Vector
What are the differences between ArrayList and LinkedList
What are the differences between an Array and a List

LinkedList has faster insertion and deletion, however traveling through an ArrayList is faster.
LinkedList implements queue interface.

// The difference between HashSet and LinkedHashSet is that LinkedHashSet maintains the
// order of insertion.

// Any implementation of the List interface would maintain the insertion order.

// ArrayList implements RandomAccess, a marker interface.  It support fast,
// almost constant time access.  Insertion and Deletion are slower compared to
// LinkedList.

Vector implements List and RandomAccess.  It is thread-safe using synchronized methods.
Vector are basically ArrayList, but Vector is thread-safe.

LinkedList implements List and Queue.  Elements are doubly linked (forward and backward).
LinkedList is an ideal choice to implement Stack and Queue.  Iteration is slower than ArrayList.
LinkedList offer fast insertion and deletion.
LinkedList is a bit slower compared to ArrayList for iteration.
LinkedList implements the Queue interface also.  So it supports method like peek(), poll(), and remove()

// Set does not allow duplication
// Map is typically considered as part of Collection, however it does not 
// extends the Collection interface.
// Map is key/value pair.

// To implement a sorting function create a class that implements the
// Comparator interface, and provide concrete implementation for the 
// compare method.

interface Collection extends Iterable {
}

interface Set extends Collection {
}

interface List extends Collection {
}

interface Queue extends Collection {
}

interface Map<K,V> {
}

add
remove
size
isEmpty()
clear()
contains(obj)
containsAll(anotherCollection)
addAll(anotherCollection)
removeAll(anotherCollection)
retainAll(anotherCollection)

// List
get(int index)
set(int index, value)
add(int index, value)
indexOf(value)
lastIndexOf(value)
addAll(int startIndex, anotherCollection)
remove(int index)

interface SortedSet extends Set;
subSet(fromELementObt, toElementObj)
headSet(toElementObj)
tailSet(fromElementObj)
first()
last()

interface NavigatableSet extends SortedSet;
lower(E e) // Find nearest value lower than the given value
floor(E e)
ceiling(E e)
higher(E e) // Find nearest value higher than the given value
pollFirst()
pollLast()

class HashSet implements Set
// unordered, unsorted - iterates in random order
// uses hashCode()

class LinkedHashSet implements Set
// ordered - iterates in order of insertion
// unsorted
// uses hashCode()

class TreeSet implements Set, NavigatableSet
// TreeSet does not maintain the order of insertion, but it stores the elements in the sorted order.

// Map
isEmpty()
containsKey(key)
containsValue(value)
get(key)
put(key,value)
remove(key)
putAll
clear()
keySet
values()
equals

// Entry
getKey
getValue
setValue

interface SortedMap extends Map
// SortedMap orders based on the keys.  Comparator can be provided at
// the map creation time
subMap
headMap
tailMap
firstKey
lastKey

interface NavigatableMap extends SortedMap
// A SortedMap extends with navigation methods reporting closest match

class HashMap implements Map
// unsorted, unordered
// key's hashCode() is used

class Hashtable implements Map
// Synchronized, thread-safe version of HashMap
// unsorted, unordered
// use hashCode
// HashMap allows a key with null value.  Hashtable doesn't

class LinkedHashMap implements Map
// insertion order is maintained
// slower insertion and deletion
// faster iteration

class TreeMap implements Map, NavigatableMap
// sorted order is maintained

// Queue
add
offer
remove
poll
element
peek

interface BlockingQueue extends Queue;

Comparator reverseComparator = new Comparator<Integer>() {
  public int compare(Integer param1, Integer param2) {
    return param1 - param2;
  }
}

PriorityQueue
// Using default constructor - uses natural ordering of numbers
// Smaller numbers have higher priority
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License