Java Collections Framework (JCF) Tutorial

Overview of Java Collections Framework (JCF):

Hope these eloquent tutorials on this Java Series for beginners are really helpful in enriching your knowledge in Java.

Our previous tutorial briefed more on the basic I/O operations in Java. In this tutorial we will see about Java Collections Framework (JCF) in detail.

Java Collections Framework (JCF) contains interfaces, abstract classes, and classes which can provide the architecture to store and manipulate a group of objects.

Watch the below Video tutorial on:

Knowledge of Arraylist in Java:

Set Interface & Queue Interface in Java with Real-time examples:

HashMap, TreeMap and HashTable in Java:

Java Collection Interface

Comparison between List, Set, and Queue:

ListSetQueue
Insertion order is maintainedInsertion order is not maintained in Hash setInsertion order is maintained.
Can contain duplicate elementsCannot contain duplicate elementsCan contain duplicate elements.
Insertion and removing of array can be done for any index.Remove the specified element.Only the last inserted element can be popped out. Also, insertion of elements happens at end.

Array List Class

In Array, the memory allocation is fixed. But in ArrayList, memory can be allocated dynamically. This ArrayList class maintains the insertion order and you can insert duplicate elements.

Demo program for Array List:

 public class ArrayListDemo {

public static void main(String[] args) {

ArrayList li = new ArrayList ();

li.add(1);
li.add(2);
li.add(3);
li.remove(0);

for (Integer temp : li) {
System.out.println(temp);
}
System.out.println("==========");

ArrayList l = new ArrayList();
l.add("text1");
l.add("text2");
l.add("text3");

for (String temp : l) {
System.out.println(temp);
}
System.out.println("==========");
ArrayList al=new ArrayList();
al.add(1);
al.add(2);
al.forEach((a)->;System.out.println(a));
}
} 

Array List Class

LinkedList Class

Linked List data structure contains nodes and this node will contain two parts:

  1. Data
  2. Reference to the next element

The first node is not a separate node. It contains only the reference and is called a head. The last node is null.

Linked List Class

Demo Program:

 public class LinkedListDemo {

public static void main(String[] args) {

LinkedList list =new LinkedList();

list.add(22);
list.add(44);
list.add(46);
list.add(46);
list.add(46);

for(Integer temp:list)
{

System.out.println(temp);

}

Iterator it =list.iterator();

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

System.out.println("==========");
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
} 

LinkedList Class demo

Queue

A queue is first in first out (FIFO) data structure. If you call the remove method, always the first inserted element will be removed from the queue. So, Queue is used in real time application where the data has to be retrieved in the inserted order.

Example Program:

 public class QueueDemo {

public static void main(String[] args) {
PriorityQueue pq=new

PriorityQueue();
pq.add(1);
pq.add(2);
pq.add(3);
pq.add(4);
pq.add(5);
pq.add(6);
pq.add(7);

for(int temp : pq)
{
System.out.println(temp);
}

pq.remove();
System.out.println("queue after removing a element");
System.out.println(pq);
}
} 

Queue

Set

A set collection will not allow duplicate elements. You can use a set data structure, when you want to process the collection of data without duplication, and when there is no need to maintain the insertion order.

Demo Program:

 public class demohashset {
public static void main (String[] args){

HashSet hs=new

HashSet();

hs.add(23);
hs.add(24);
hs.add(25);
hs.add(26);
hs.add(27);
hs.add(28);
hs.add(293);
hs.add(203);
hs.add(263);
hs.add(243);
hs.add(243);

(int temp:hs)
{

System.out.println(temp);
}

hs.remove(24);
hs.remove(23);

System.out.println("===============");
System.out.println("after removing a element ::::");
System.out.println("===============");
hs.forEach((temp)->System.out.println(temp)); ;
}
} 

Set demo

MAP Interface

  • When you want process collection of elements with the key and value pair, then you can use map data structure as the map is an object which can map keys to values.
  • A map cannot contain duplicate keys.
  • The important implementations of map are HashMap, Treemap, LinkedHashMap, HashTable.

Difference between HashMap, Treemap, LinkedHashMap, and HashTable:

HashMapTreeMapLinkedHashmapHashTable
Null keys and values are allowedOnly null values allowed.Null keys and values allowed.It won’t allow null keys and values.
Not synchronizedNot synchronizedNot synchronizedsynchronized
There is no guarantee to maintain order in the iterationSorting will be done based on natural order.Insertion order will be maintainedInsertion order not maintained.

Demo Program:

 public class HashMapDemo {

public static void main(String[] args) {
HashMap<Integer, String> m=new HashMap<Integer,String>();

m.put(1, "one");
m.put(2, "two");
m.put(3, "three");
m.put(4, "four");
m.put(null, "one");
m.put(null, null);

System.out.println(m);

TreeMap<String, String> tm =new TreeMap<String, String>();

tm.put("R", "red");
tm.put("B", null);
tm.put("G", "green");
System.out.println(tm);

Hashtable<Integer, String> ht =new Hashtable<Integer, String>();

ht.put(1, "one");
ht.put(2, "two");
ht.put(3, "three");
ht.put(4, "four");

System.out.println(ht);

}
} 

MAP Interface demo

Key Points to be noted:

  • List, Queue, set interfaces extend the collection interface and this collection interface have common methods like add, remove etc.
  • Insertion order is maintained in list and a set cannot contain duplicate elements. Queue is First in First out data structure.
  • Map will contain key and value pairs. HashMap, Treemap, Hashtable, Linked HashMap are the important implementation of the map interface.

Conclusion

In this tutorial, you learned about List, Queue, Set and Map and also the ways to use these data structures in programs.


You can explore more about Reflections in JAVA in our upcoming tutorial!!!

PREV Tutorial | NEXT Tutorial