Java Collections

October 21, 2018 Off By ravi24kant

Java Collections

If you have already read data structures then java collections can be easily understood. Some data structures (set, list, queue and array) are used to organize multiple objects together in Java. These data structures are called collections in java. The collection of these data structures is called the Java collection framework. With the help of these Java collections you can perform all those operations (searching, sorting, insertion, deletion, etc.) with objects. Which you do with the normal data. Classes and interfaces have already been provided to make your work easier in the Collections Framework. These classes implement these interfaces. By expanding these classes, you can easily organize objects. You do not need to create separate data structures. All interfaces and classes of the Collections framework are generic, so the type of object you give when you create the collection. These interfaces and classes are being given below.

 

Java Collections Interface

All interfaces of the Collections Framework implement the collection interface by default. The Collection interface provides several methods that you will use to manage objects.

All these methods are listed below.

Java Collections Methods Description

Boolean add(E obj)                   ->By this method you add object to the collection.
Void clear()                                 ->This method removes all the objects of the collection when it is called.
Boolean contains(Object obj) ->With this method you can search the object in the collection. If the object is  found then this method returns true.
Boolean equals(Object obj)     ->This method compares the object passed from the object of collection call and returns true when it is equal.
Boolean isEmpty()                     ->If the caller’s collection is empty then this method returns true.

Java Collections – List Interface

ArrayList and LinkedList classes implement the list interface. These classes store objects like a list. You can also store duplicate objects in a list collection. By creating an object of these classes, you can store objects like a list.

ArrayList

ArrayList<objType> ArrList = new ArrayList<objType>();
arrList.add(obj1);
arrList.add(obj2);
arrList.add(obj3);

LinkedList

LinkedList<objType> linklist = new LinkedList<objType>();
linklist.add(obj1);
linklist.add(obj2);
linklist.add(obj3);

Vector

Vector<objType> vector= new Vector<objType>();
vector.add(obj1);
vector.add(obj2);
vector.add(obj3);

Java Collections – Set Interface

HashSet or TreeSet classes implement the set interface. Objects in the set collection are stored like a Set.
In it you cannot store duplicate objects. you can create a set collection by creating the object of the classes.

HashSet
HashSet<objType> hSet = new HashSet<objType>();
hSet.add(obj1);
hSet.add(obj2);

TreeSet
TreeSet<objType> tSet = new TreeSet<objType>();
tSet.add(obj1);
tSet.add(obj2);

LinkedHashSet
LinkedHashSet<objType> lhSet = new LinkedHashSet<objType>();
lhSet .add(obj1);
lhSet .add(obj2);

SortedSet
SortedSet<objType> stSet = new SortedSet<objType>();
stSet .add(obj1);
stSet .add(obj2);

Java Collections – Queue Interface

The PriorityQueue class executes the queue interface. Whenever you store objects in this collection, they are stored in the form of First In First Out.

PriorityQueue
PriorityQueue<objType> pQueue = new PriorityQueue<objType>();
pQueue.add(obj1);
pQueue.add(obj2);

Java – Map Interface

HashMap and TreeMap class implement the map interface. In the map collection, the data is stored in a pair of value and value. Key is used to store and access every element. The elements in the map collection are unique.

HashMap
HashMap<objType> hMap = new HashMap<objType>();
hMap.add(key,obj1);
hMap.add(key.obj2);

TreeMap
TreeMap<objType> tMap = new TreeMap<objType>();
tMap.add(key,obj1);
tMap.add(key,obj2);

Java Collections – Iterator Interface

Like the Collection interface, all the interface Iterator interface also applies by default. This interface provides the iterator (), hasNext () and next () methods. With the help of these methods you can traverse your collection.
First you call iterator () method on the collection. This returns the index of the first element of the method collection. You store this in the reference variable of the iterator interface. After this you run the loop while using the hasNext () method. And next () method traverse all the values of the collection.

ArrayList<objType> aList = new ArrayList<objType>();
aList.add(key,obj1);
aList.add(key,obj2);

Iterator<objType> itr = aList.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}