Collections Tutorial Step 1

What is the Collections Framework?
The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance. It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse. The framework is based on nine collection interfaces. It includes implementations of these interfaces, and algorithms to manipulate them.

Collection Interfaces - The primary means by which collections are manipulated.

  • Collection - A group of objects. No assumptions are made about the order of the collection (if any), or whether it may contain duplicate elements.
  • Set - The familiar set abstraction. No duplicate elements permitted. May or may not be ordered. Extends the Collection interface.
  • List - Ordered collection, also known as a sequence. Duplicates are generally permitted. Allows positional access. Extends the Collection interface.
  • Queue - A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.
  • Map - A mapping from keys to values. Each key can map to at most one value.
  • SortedSet - A set whose elements are automatically sorted, either in their natural ordering (see the Comparable interface), or by a Comparator object provided when a SortedSet instance is created. Extends the Set interface.
  • SortedMap - A map whose mappings are automatically sorted by key, either in the keys' natural ordering or by a comparator provided when a SortedMap instance is created. Extends the Map interface.
  • BlockingQueue - A Queue with operations that wait for the queue to become non-empty when retrieving an element, and that wait for space to become available in the queue when storing an element. (This interface is part of the package java.util.concurrent.)
  • ConcurrentMap - A Map with atomic putIfAbsent, remove, and replace methods. (This interface is part of the package java.util.concurrent.)

General Purpose Implementations - The primary implementations of the collection interfaces.

  • HashSet - Hash table implementation of the Set interface. The best all-around implementation of the Set interface.
  • TreeSet Red-black tree implementation of the SortedSet interface.
  • LinkedHashSet - Hash table and linked list implementation of the Set interface. An insertion-ordered Set implementation that runs nearly as fast as HashSet.
  • ArrayList - Resizable-array implementation of the List interface. (Essentially an unsynchronized Vector.) The best all-around implementation of the List interface.
  • LinkedList - Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Can be used as a double-ended queue (deque). Also implements the Queue interface. When accessed via the Queue interface, LinkedList behaves as a FIFO queue.
  • PriorityQueue - Heap implementation of an unbounded priority queue.
  • HashMap - Hash table implementation of the Map interface. (Essentially an unsynchronized Hashtable that supports null keys and values.) The best all-around implementation of the Map interface.
  • TreeMap Red-black tree implementation of the SortedMap interface.
  • LinkedHashMap - Hash table and linked list implementation of the Map interface. An insertion-ordered Map implementation that runs nearly as fast as HashMap. Also useful for building caches.


Legacy Interfaces

  • Enumeration - Define methods by which you can enumerate the elements in a collection of objects. Enumeration is synchronized.

Legacy Implementations - Older collection classes have been retrofitted to implement the collection interfaces.

  • Vector - Synchronized resizable-array implementation of the List interface with additional "legacy methods". Vector holds any type of objects, it is not fixed length and vector is synchronized. We can store primitive data types as well as objects. Default length of vector is up to 10.
  • Hashtable - Synchronized hash table implementation of the Map interface that does not allow null keys or values, with additional "legacy methods". HashTable stores key/value pairs in hash table, HashTable is synchronized when using hash table you have to specify an object that is used as a key, and the value that you want to linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored with the table. Use Hash Table to store large amount of data, it will search as fast as vector. Hash Table store the data in sequential order.

Abstract Implementations - Skeletal implementations of the collection interfaces to facilitate custom implementations.

  • AbstractCollection - Skeletal Collection implementation that is neither a set nor a list (such as a "bag" or multiset).
  • AbstractSet - Skeletal Set implementation.
  • AbstractList - Skeletal List implementation backed by a random-access data store (such as an array).
  • AbstractSequentialList - Skeletal List implementation backed by a sequential-access data store (such as a linked list).
  • AbstractQueue - Skeletal Queue implementation.
  • AbstractMap - Skeletal Map implementation.

AddThis Feed Button
Please provide your feedback by sending an email to 1234java.blogspot@gmail.com. This would help me making the site more helpful. Thanks in advance.

DISCLAIMER:

The content provided in this article/site is not warranted or guaranteed by 1234java.blogspot.com. The content provided is intended for educational purposes in order to introduce to the reader key ideas, concepts. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials.