Collections

Java has the collection hierarchy, that includes all types of collections except maps. Maps has own hierarchy.

All collection also implement the Iterable interface, that allows to use collection in the "foreach" statement.

With regard to multithreading, collections can be divided into several kinds:

  • ordinary, that must be used inside a single thread
  • synchronous, that have atomicy operations
  • blocking, that have blocking operations (read more)
  • concurrent, that allows multiple threads to access data independently without blocking each other (read more)

Collection

Collection is the root interface for all collections except maps. JDK does not provide any direct implementations.

method description
add(e) Adds element into collection.
addAll(src) Adds all elements from the specified collection.
clear() Removes all of the elements from this collection.
contains(e) Returns true if this collection contains the specified element.
containsAll(c) Returns true if this collection contains all of the elements in the specified collection.
isEmpty() Returns true if this collection empty.
iterator() Returns an iterator over the elements in this collection.
parallelStream() Returns a possibly parallel Stream with this collection as its source.
remove(e) Removes a specified element, if it is present. Returns true if element was removed.
removeAll(src) Removes from this collection all elements that contain in the specified collection.
removeIf(filter) Removes all of the elements of this collection that satisfy the given predicate.
retainAll(src) Removes from this collection all elements that not contain in the specified collection.
size() Returns the number of elements in this collection.
stream() Returns a sequential Stream with this collection as its source.
toArray() Returns an array containing all of the elements in this collection.
toArray(dst) Returns an array containing all of the elements in this collection. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

iterators

All collections implement the Iterable interface, that provide us an iterator.

Iterator allows to traverse the collection, access the element and remove elements of the collection.

method description
hasNext() Returns true if the iteration has more elements.
next() Returns the next element in the iteration.
remove() Removes from the underlying collection the last element returned by this iterator (optional operation).
// using iterator directly
Iterator<String> iterator = myCollection.iterator(); 

while (iterator.hasNext()){ 
    System.out.println(iterator.next());
}

// using foreach statement
for (String e: myCollection){
    System.out.println(e);
}

Although Java uses an Iterator implicitly in the foreach statement, in this case, you have no way to remove elements.

overview the collection hierarchy

class description
Collection Root interface for all collections except maps. JDK does not provide any direct implementations.
Iterable Auxiliary interface, that allows to get an iterator over all elements of collection and to use collection in "foreach" statement.
AbstractCollection Provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.

To implement an unmodifiable collection, you need provide the iterator() and size() methods. The iterator returned by the iterator method must implement hasNext and next.

To implement a mutable collection, you need to override the add () and remove () methods.

List

Extension of Collection to represent a list. List allows to access to element by index, supports search an element and provides a special iterator - ListIterator.

ArrayList, Vector, Stack, LinkedList classes are implemetations of this interface.

Set

Extension of Collection to represent a set. Set is a collection that contains no duplicate elements.

HashSet, LinkedHashSet, TreeSet, EnumSet classes are implemetations of this interface.

Queue

Extension of Collection to represent a queue. Queue provides insertion, extraction, and inspection operations.

ArrayDeque, LinkedList classes are implementations of this interface.

Deque

Extension of Queue to represent a deque. Deque provides element insertion and removal at both ends.

ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList classes are implementations of this interface.