Basic things you should know about Java Collections

Jackie Vaswani
3 min readAug 12, 2020

As the name suggests collection is the group of objects & java collection framework provides implementations that can hold the group of objects.

some random image from google because people want to read an article which has good visual image. LOL

Java collection is divided into 4 main sections-

  1. Set
  2. List
  3. Queue
  4. Map

SET — It is a data structure that stores unique objects. In java, Set is an interface & it has 3 implementations-

  1. HashSet- Set which is places objects unordered & unsorted. HashSet is used when the order of insertion/arrangement of objects is not important. Internally it uses HashMap to store the elements.
  2. LinkedHashSet — Set in which order of insertion is preserved.
  3. TreeSet — Set in which objects are placed in sorted order.

LIST- It is a data structure where we can store ordered collection of objects. It can have duplicate values. The list is an interface in java & below are the implementations-

  1. ArrayList — Internally it stores data in a dynamic array. ArrayList is used when elements are accessed more frequently than insertion/deletion
  2. LinkedList- Internally it stores data in a LinkedList. It is used when insertion/ deletion operation are used more frequently than the read operation.
  3. Vector- This is also used array as an internal data structure. Difference b/w ArrayList & Vector is that Vector is thread-safe ie all the functions are synchronized.

Queue- Linear data structure where data is stored in LIFO order

  1. LinkedList — LinkedList implements both the interface List & Queue
  2. PriorityQueue- This is a Queue where every element inserted is also associated with priority & element will most priority will be removed first. Internally it uses the Heap data structure.

Map — It is a collection of key-value pair objects. It has 4 implementations-

  1. HashMap- data is stored as unorder & unsorted
  2. LinkedHashMap- order of insertion is preserved
  3. Hashtable- Legacy Map which is thread-safe & data is stored as unorder & unsorted
  4. TreeMap- Keys are stored in sorted order. Internally used is Red-black tree for storing data.

CODE -

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{

// Set uses hashcode & equals method
Set <Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(1);
System.out.println(set.size());
set.remove(1);
System.out.println(set.size());


// ArrayList — remove, contains uses equals method to check whether element is present or not
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list.get(0));
list.remove((int)0); // remove at position 0
System.out.println(list.get(0));
list.remove((Integer)2); // remove Integer Object 2
System.out.println(list.size());

list.add(10);
list.add(1);
list.add(100);
Collections.sort(list);

/* uses compareTo method & default order is ascending */

System.out.println(Arrays.toString(list.toArray()));

Collections.sort(list, (a, b) -> b-a );

/*

Custom comparator is passed as a parameter which will be used to make decision for swap.

Eg- if 2nd object b is greater than first object a then return positive to create desc order

*/

System.out.println(Arrays.toString(list.toArray()));


// HashMap uses hashcode & equals method
Map<Integer, Integer> map = new HashMap<>();
map.put(1,2);
System.out.println(map.get(1));
System.out.println(map.containsKey(1));
map.remove(1);
System.out.println(map.size());
map.put(10,20);
map.putIfAbsent(10,10);
System.out.println(map.get(10));
System.out.println(map.getOrDefault(12, 0));


// Queue — LinkedList
// Priority Queue also work the same way but it removes most priorty object which it calculates using compareTo method
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
System.out.println(queue.element());
queue.remove();
System.out.println(queue.element());

}
}

OUTPUT -

2
1
1
2
0
[1, 10, 100]
[100, 10, 1]
2
true
0
20
0
1
2

--

--