Java Tutorials – Collections



Java Tutorials – Collections

()

Based on Java SE 6

• Core Collection interfaces:

[pic]

• Iterator.remove() is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.

• Collection bulk operations:

o containsAll

o addAll

o removeAll

o retainAll

o clear

• Set:

o Adds a stronger contract on the behavior of the equals() and hashCode() operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.

o SortedSet, NavigableSet

o HashSet, TreeSet, LinkedHashSet

o EnumSet, ConcurrentSkipListSet, CopyOnWriteArraySet

• Map:

o SortedMap, NavigableMap,

o ConcurrentMap, ConcurrentNavigableMap

o HashMap, TreeMap, LinkedHashMap, Hashtable

o Properties, EnumMap, IdentityHashMap, WeakHashMap

o ConcurrentHashMap, ConcurrentSkipListMap

o LinkedHashMap – insertion / access order, removeEldestEntry()

o Collections views:

▪ keySet

▪ values

▪ entrySet

• List:

o Positional access

o Search

o Range-view

o RandomAccess, ListIterator

o ArrayList, LinkedList, Vector

o Stack, CopyOnWriteArrayList

• Queue:

o Insert: add / offer (throws exception / returns special value)

o Remove: remove / poll

o Examine: element / peek

o Collections.asLifoQueue(Deque)

o Deque, BlockingQueue, BlockingDeque

o LinkedList, LinkedBlockingQueue, LinkedBlockingDeque

o ConcurrentLinkedQueue, DelayQueue, SynchronousQueue

o ArrayBlockingQueue, ArrayDeque

o PriorityBlockingQueue, PriorityQueue

o PriorityQueue (based on heap) – head of the queue is the least element with respect to the specified ordering

• Comparator/Comparable consistent with equals:

( pareTo( o2 ) == 0 ) o1.equals( o2 )

Required by sorted collections – use compare() instead of equals():

o SortedSet: ConcurrentSkipListSet, TreeSet

o SortedMap: ConcurrentSkipListMap, TreeMap

• Collections.nCopies – immutable multiple-copy List

• Collections.singleton – immutable singleton Set

• Algorithms:

o sort (optimized mergesort, stable)

o shuffle

o reverse

o rotate

o swap

o replaceAll

o fill

o binarySearch

o indexOfSublist

o lastIndexOfSublist

o frequency

o disjoint

• Upward compatibility (old => new):

o Array => List : Arrays.asList

o Enumeration => List : Collections.list()

o Vector => List : already

o Hashtable => Map : already

• Backward compatibility (new => old):

o Collection => Array : c.toArray()

o Collection => Enumeration : Collections.enum()

o Collection => Vector : new Vector(c)

o Map => Hashtable : new Hashtable(m)

• Custom Collection implementations:

o AbstractCollection, AbstractSet

o AbstractQueue, AbstractMap

o AbstractList – backed by a random access data store (like array)

o AbstractSequentialList – backed by a sequential access data store (like linked list)

Collections Framework Enhancements

• Java 5:

o Queue: PriorityQueue, ConcurrentLinkedQueue, LinkedList

o BlockingQueue: LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue, DelayQueue, SynchronousQueue

o ConcurrentMap: ConcurrentHashMap

o CopyOnWriteArrayList, CopyOnWriteArraySet

o EnumSet, EnumMap

o Collections:

▪ checkedXyz, emptyXyz

▪ reverseOrder(Comparator)

▪ frequency, disjoint

▪ addAll (array => collection)

o Arrays:

▪ hashCode, toString

▪ deepEquals, deepHashCode, deepToString

• Java 6:

o NavigableSet: TreeSet, ConcurrentSkipListSet

o NavigableMap / ConcurrentNavigableMap:

TreeMap, ConcurrentSkipListMap

o Deque / BlockingDeque: ArrayDeque, LinkedBlockingDeque

o Collections:

▪ newSetFromMap (returns set backed by the specified map)

▪ asLifoQueue(Deque)

o Arrays:

▪ copyOf, copyOfRange

Collections Frameworks



• Lock-Free Hash Table

o

o

• Commons Collections

o

o

o Bag – collections having a number of copies of each object

o BidiMap – bi-directional Map (can be looked up from value to key)

o Composite collections

o Old – some implementations already available in Java 6

• fastutil

o

o

o Type-specific maps, sets, lists and queues

o Primitive and binary type I/O

• GNU Trove

o

o

o Collections and hashing support for primitive types

o Primitive type Iterators

o Used by JBoss

• Mango

o

o

o STL-inspired iterators, algorithms and functions

• ObjectSpace – JGL

o

o

o

o

o Java Collections alternative

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download