-> Collections in java:

Java collections represents a single unit of objects i.e a group.

ListInterface:

List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

ArrayList: Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface.

Allows duplicate , maintains insertion order , slow , random access , non synchronised.

LinkedList: Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

Allows duplicate , maintains insertion order , fast , non synchronised , can be used as list , stack or queue.

SetInterface:

Set Interface implements collections interface.

It has three classes HashSet , LinkedHashSet , TreeSet

HashSet: Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.
  • Maintains ascending order

List can contain duplicate elements whereas Set contains unique elements only.

LinkedHashSet: Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface.

The important points about Java LinkedHashSet class are:

  • Contains unique elements only like HashSet.
  • Provides all optional set operations, and permits null elements.
  • Maintains insertion order.

TreeSet: Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. The objects of TreeSet class are stored in ascending order.

The important points about Java TreeSet class are:

  • Contains unique elements only like HashSet.
  • Access and retrieval times are quiet fast.
  • Maintains ascending order.

QueueInterface:

Java Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.

PriorityQueue , Deque ,ArrayDeque.

PriorityQueue: The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner. It inherits AbstractQueue class.

Java Deque Inteface:

Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for “double ended queue”.

Array Deque class: The ArrayDeque class provides the facility of using deque and resizable-array. It inherits AbstractCollection class and implements the Deque interface.

The important points about ArrayDeque class are:

  • Unlike Queue, we can add or remove elements from both sides.
  • Null elements are not allowed in the ArrayDeque.
  • ArrayDeque is not thread safe, in the absence of external synchronization.
  • ArrayDeque has no capacity restrictions.
  • ArrayDeque is faster than LinkedList and Stack.

Java Map Interface:

A map contains values on the basis of key i.e. key and value pair. Each key and value pair is known as an entry. Map contains only unique keys.

Map is useful if you have to search, update or delete elements on the basis of key.

There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, LinkedHashMap and TreeMap. The hierarchy of Java Map is given below:

HashMap

HashMap is the implementation of Map but it doesn’t maintain any order.

LinkedHashMap

LinkedHashMap is the implementation of Map, it inherits HashMap class. It maintains insertion order.

TreeMap

TreeMap is the implementation of Map and SortedMap, it maintains ascending order.

Map doesn’t allow duplicate keys, but you can have duplicate values. HashMap and LinkedHashMap allows null keys and values but TreeMap doesn’t allow any null key or value.

Map can’t be traversed so you need to convert it into Set using keySet() or entrySet() method.

HashMap: Java HashMap class implements the map interface by using a hashtable. It inherits AbstractMap class and implements Map interface.

The important points about Java HashMap class are:

  • A HashMap contains values based on the key.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.

LinkedHashMap: Java LinkedHashMap class is Hash table and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.

The important points about Java LinkedHashMap class are:

  • A LinkedHashMap contains values based on the key.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It is same as HashMap instead maintains insertion order.

TreeMap: Java TreeMap class implements the Map interface by using a tree. It provides an efficient means of storing key/value pairs in sorted order.

The important points about Java TreeMap class are:

  • A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
  • It contains only unique elements.
  • It cannot have null key but can have multiple null values.
  • It is same as HashMap instead maintains ascending order.

For Iteration of collection we use Iterator or for each loop.

Example of using iterator method for iterating over collection is :

List<Integer> list=new ArrayList<>();

List.add(1);

List.add(2);

List.add(3);

Iterator <Integer> itr=list.iterator();

while(its.hasNext()){

System.out.println(“Iterating over list ”+itr.next());

}

Using For Each loop for iterating over collection:

for(Integer x: list){

System.out.println(x);

}

Software Developer