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

Posted

in

by

Tags:


Related Posts

Comments

24 responses to “Complete Java Collection tutorial for the beginner”

  1. xlkaksje todd asde Avatar
    xlkaksje todd asde

    Perfect work you have done, this web site is really cool with good info.

  2. Pmvignes Avatar
    Pmvignes

    Good to see all the basic information in the single page. 

    1. JitendraZaa Avatar
      JitendraZaa

      Thanks

  3. hemalathav Avatar
    hemalathav

    ur notes is amazing

    1. JitendraZaa Avatar
      JitendraZaa

      Thanks Hemal

  4. Rahul Avatar
    Rahul

    great article !!!

  5. vasantha aarabhi Avatar
    vasantha aarabhi

    really usefull and easy to understand. thank you

  6. Hanumant Bandgar Avatar
    Hanumant Bandgar

    Really nice for Developer

  7. lin Avatar
    lin

    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.

  8. vikas goyal Avatar
    vikas goyal

    Good tutorial

  9. Java Training In Chennai Avatar

    It is not type safe collection. Currently java supports type safe collections (generics)

    1. JitendraZaa Avatar
      JitendraZaa

      All above are Type Safe only

  10. yesh Avatar
    yesh

    really easily learn collection this website
    nice website

  11. RAGHU Avatar
    RAGHU

    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

  12. hasi_wk Avatar

    thank you sire

  13. Daniel Charlie Avatar

    nice article

  14. Rajesh n Avatar

    Valuable post useful for everyone. Keep on sharing.

  15. Saurabh Tomer Avatar

    Thanks for sharing valuable information.

  16. teja aswani Avatar

    Wow it was a very nice article so thankful to you for this kind of information provided for us

  17. DevOps Course Avatar

    Java is a broad language to learn. This is really useful content for beginners. Keep posting content like this.

  18. Eden Wheeler Avatar
    Eden Wheeler

    great post

  19. Vivek Garg Avatar
    Vivek Garg

    Very informative!

  20. Vivek Garg Avatar
    Vivek Garg

    Thank you for such an interesting post! Reading it taught me a lot. Your writing is highly fascinating, and I appreciate how you divided the complex issue into manageable portions.

  21. Azure Trainings in Hyderabad Avatar

    Thank you for such an interesting post! Reading it taught me a lot. Your writing is highly fascinating, and I appreciate how you divided the complex issue into manageable portions.

Leave a Reply to xlkaksje todd asdeCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading