Java Collections Framework

The Java Collections Framework is a set of classes and interfaces that implement commonly reusable collection data structures. A collection, as its name implied, is simply an object that holds a collection (or a group, a container) of objects. Each item in a collection is called an element. A framework, by definition, is a set of interfaces that force you to adopt some design practices. A well-designed framework can improve your productivity and provide ease of maintenance. All the operations that you perform on a data such as searching, insertion, manipulation, deletion, sorting etc. can be performed by Java Collections.

Java Collections Framework

The Java Collections Framework package (java.util) contains:

  1. A set of interfaces (eg: Collection, Set, List, Map, Queue)
  2. Implementation classes (eg: ArrayList, LinkedList, HashMap, HashSet, TreeMap)
  3. Algorithms (eg: sorting and searching)

Set, List and Map

Set, List and Map are three important interfaces of Java collection framework and it’s differences are one of the frequently asked java interview question.

    • List in java provides ordered and indexed collection which may contain duplicates.
    • The Set interface provides an unordered collection of unique objects, i.e. Set doesn’t allow duplicates.
    • Map provides a data structure based on key-value pair and hashing

List Vs Set Vs Map

1) Duplicity: List allows duplicate elements, any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes. Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements. Map stored the elements as key & value pair and it doesn’t allow duplicate keys while it allows duplicate values.

2) Null values: List allows any number of null values, set allows single null value at most whereas map can have only one null key at most and any number of null values.

3) Order: List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order. Similar to Set Map also doesn’t stores the elements in an order, however few of its classes does the same. For e.g. TreeMap sorts the map in the ascending order of keys.

Now, let’s checkout some examples:

Collection by Example

ArrayList


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task1again;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
*
* @author Sujan Maka
*/
public class ArrayListExample {

public static void main(String[] args) {
List student = new ArrayList<>();
student.add("james");
student.add("lives");
student.add("in");
student.add("london");

System.out.println(student); // [james, lives, in, london]

// Get a "iterator" instance from List to iterate thru all the elements of the List
Iterator iter = student.iterator();
while (iter.hasNext()) { // any more element
// Retrieve the next element, explicitly downcast from Object back to String
String str = (String) iter.next();
System.out.println(str);
}
}
}

LinkedList


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task1again;

import java.util.LinkedList;
import java.util.List;

/**
*
* @author Sujan Maka
*/
public class LinkedListExample {
public static void main(String[] args) {
List<String> list = new LinkedList<>();

list.add("Item1");
list.add("Item5");
list.add("Item3");
list.add("Item6");
list.add("Item2");

System.out.println("Linked List Content: " +list);

Object firstvar = list.get(0);
System.out.println("First element: " +firstvar);
list.set(0, "Changed first item");
Object firstvar2 = list.get(0);
System.out.println("First element after update by set method: " +firstvar2);

list.add(0, "Newly added item");
list.remove(2);
System.out.println("Final Content: " +list);
}

}

HashMap


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task1again;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
*
* @author Sujan Maka
*/
public class HashMapExample {
public static void main(String[] args) {
Map mapA = new HashMap();
mapA.put("key1", "element 1");
mapA.put("key2", "element 2");
mapA.put("key3", "element 3");

String element = (String) mapA.get("key1");
System.out.println(element);

Iterator iterate = mapA.keySet().iterator();
while(iterate.hasNext()){
Object key = iterate.next();
Object value = mapA.get(key);

System.out.println(key);
System.out.println(value);
}

mapA.remove("key1");
for(Object key : mapA.keySet()){
Object value = mapA.get(key);
System.out.println(key);
}

}
}

HashSet


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task1again;

import java.util.HashSet;
import java.util.Iterator;

/**
*
* @author Sujan Maka
*/
public class HashSetExample {

public static void main(String[] args) {
HashSet<String> names = new HashSet<>();
names.add("shyam");
names.add("khan");
names.add("khan");
names.add("srk");

Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
System.out.println(item);
}
for (String item : names) {
System.out.println(item);
}
}
}

 

Now you’ve learned basics about collection framework in java. You can do lots of things with java collection framework. I’ve done simple project using collection framework in next tutorial. To learn more about collections in java you can always refer to official site. Now let’s check out this simple application using collection framework.