Collection In Java

Vaibhav Bharadwaj
2 min readNov 15, 2020

--

A collection is an object that can hold references to other objects.

Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

Iterator in Java

Iterator allows us to traverse the collection, access the data element, and remove the data elements of the collection.

List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Iterator<String> itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
itr.remove();
System.out.println(list);

OUTPUT

[1,2,3]

List:-

List is an interface which can store the ordered collection of objects and duplicate values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

List <data-type> list1= new ArrayList();
List <data-type> list2 = new LinkedList();
List <data-type> list3 = new Vector();

ArrayList:-

The ArrayList class uses a dynamic array where it can contain duplicate elements and maintains insertion order.

import java.util.*;
public class ArrayListExample1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();
list.add(“1”);
list.add(“2”);
list.add(“3”);
list.add(“4”);
System.out.println(list);}}
OUTPUT
[1 2 3 4]

LinkedList

LinkedList class uses a doubly linked list to store the elements. It can maintain insertion order.

It’s faster than an array in insertion and deletion.

It can be used as a list, stack, or queue.

Set:-

Set represents the unordered set of elements that doesn’t allow us to store the duplicate items. We can store at most one null value in Set.

Set is implemented by HashSet, LinkedHashSet, and TreeSet.

Set<data-type> s1 = new HashSet<data-type>();
Set<data-type> s2 = new LinkedHashSet<data-type>();
Set<data-type> s3 = new TreeSet<data-type>();

TreeSet

It inherits AbstractSet class and implements the NavigableSet interface

  • It contains unique elements and doesn’t allow null element.
  • Java TreeSet class maintains ascending order
public static void main(String args[]){
TreeSet<String> al=new TreeSet<String>();
al.add(“Ravi”);
al.add(“Vaibhav”);
al.add("Bhanu");
System.out.println(al);
OUTPUT
[Bhanu, Ravi, Vaibhav]

Queue:-

It is an ordered list of objects with its use to insert elements at the end of the list and deleting elements from the start of the list, it follows the FIFO or the First-In-First-Out.

Queue<data-type> queue = new PriorityQueue<data-type> ();

Queue<data-type> queue = new PriorityQueue<data-type> ();

HashMap:-

HashMap allows us to store key and value pairs, where keys should be unique. HashMap is non synchronized and maintains no order.

HashMap<data-type,data-type> map=new HashMap<data-type,data-type>();
import
java.util.*;
public class HashMapExample1{public static void main(String args[]){HashMap<Integer,String> map=new HashMap<Integer,String>(); HashMap<Integer,String>map.put(1,"Rahul"); map.put(2,"Sumit");map.put(4,"Vaibhav");System.out.println("Iterating Hashmap...");for(Map.Entry m : map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

--

--

No responses yet