Complete Java Collection tutorial for the beginner

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 16 Comments on Complete Java Collection tutorial for the beginner

Complete JAVA Collection tutorial for the beginner

Collection represents the group of objects. Depending on the method of storing and retrieving, collections are basically divided into three parts – Set, Map and List. Where Set does not contain duplicate values, Map contains key value type of data whereas List can have a duplicate values stored in it sequentially. This framework is provided in “java.util” package. Collection is the parent interface of all collections in java.

Following list describes the core collection interfaces.

 

Java Collection Interfaces
Java Collection Interfaces

Collection “” the root of the collection hierarchy. A collection represents a group of objects known as its elements. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn’t provide any direct implementations of this interface but provides implementations of more specific sub interfaces, such as Set and List.

Set “” a collection that cannot contain duplicate elements

List “” an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).

Queue “” a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements’ natural ordering.

Map “” an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you’ve used Hashtable, you’re already familiar with the basics of Map.

 


Concrete Classes :

Java Collection interfaces and concrete classes
Java Collection interfaces and concrete classes

HashSet :
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

LinkedHashSet :
Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation).

Interface SortedSet:
A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap).

TreeSet:
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (seeComparable), or by the comparator provided at set creation time, depending on which constructor is used.


ArrayList:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Vector:
This class is roughly equivalent to ArrayList except it is Synchronized.

LinkedList:
Linked list implementation of the List and Queue interfaces. Implements all optional operations, and permits all elements (including null).


JAVA Map interface and concrete classes
JAVA Map interface and concrete classes

HashMap:
The HashMap class is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.

HashTable:
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

LinkedHashMap:
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.

TreeMap:
This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key’s class (seeComparable), or by the comparator provided at creation time, depending on which constructor is used.


Note :
To synchronize the Set Concrete classes, use below code:

Set s = Collections.synchronizedSet(CONCREATECLASS_OBJECT);

To Synchronize the List Concreate classes :

List list = Collections.synchronizedList(CONCREATECLASS_OBJECT);

Collection Summary Chart
Collection Summary Chart

Related posts

16 thoughts on “Complete Java Collection tutorial for the beginner”

  1. your diagram of hash table and description are different. In description you say it permits nulls.
    I’m tired but I think I am reading it right.

  2. Thank you very much for the information and explanation with images.

    Hashtable wont allow null as a key and null as a value even, it throws the NullPointerException in both the scenarios for example when we try to put(null,123) or put(123,null) but the same combination works with Hashmap

Leave a Reply

Your email address will not be published. Required fields are marked *