Java Collections

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@wahidurrahman·
0.000 HBD
Java Collections
#### What Will We Learn from this tutorial ?
In this Tutorial , We are learning the basic knowledge of Java Collections. 
#### Collections: Collections represent a single unit of object. We perform on data such as searching ,sorting, Insert,update,delete can be performed by Java Collections.
- We will learn from this tutorial about Java Linked List and Array List.
- Also learn Java Map Interface and Java Hash Set.
- Java Tree Map and Sorting Collections .

#### Requirements of Java Collections 
Write here a bullet list of the requirements for the user in order to follow this tutorial.

- Basic knowledge of Java like class, Object,data type
- Basic Concepts of Object Oriented Programming (OOP).
- Array, Link List, Queue

#### Difficulty
There is no difficulty in this tutorial . It is primary level contents and easy.

- Basic


#### Tutorial Contents

Collections in java is a framework that provides an architecture to store and manipulate the group of objects. It Provides many Interfaces and classes. The Collections framework was designed for High performance. We mainly focus the basic concepts of collections .

#### Java Array List : Java Array List class contain duplicate elements . It uses a dynamic array for storing the elements. It is maintain Insertion Order. It allows random access. There is an array list ( ) method. It is used to build an empty array list. 
We can see an example of Java array list to the given below :
```
import java.util.*;  
class arraylist{  
 public static void main(String args[]){  
  ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
//Adding object in arraylist  
  list.add("wahid");
  list.add("mursalat");  
  list.add("tanvir");  
  list.add("safat");  
  
  Iterator itr=list.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
```
OutPut : After running the following program, we get the output -
![1.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516455512/fpaflymkkieixqwuoz8p.png)

#### Java LinkedList : Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. java linkedlist class is  non synchronized. It is also contains duplicate elements. It can be used as list, stack ,queue .
Let's show an example of Java Linkedlist-
```
import java.util.*;  
public class linkedlist{  
 public static void main(String args[]){  
  
  LinkedList<String> al=new LinkedList<String>();  
  al.add("wahid");  
  al.add("mursalat");  
  al.add("tanvir");  
  al.add("safat");  
  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
```
Output :  After running the program , we will get the following Output :
![2.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516456380/o0nqpxbbtl3axhnrsjoi.png)

#### Java List Interface : Java List interface is the sub Interface of collection.It contains methods to insert and delete elements in index basis. 

    Here we can see an example of the Java List Interface-
```
import java.util.*;  
public class listinterface{  
public static void main(String args[]){  
ArrayList<String> al=new ArrayList<String>();  
al.add("wahid");  
al.add("mursalat");  
al.add("mominul");  
al.add(1,"salim");  
System.out.println("Element at 3rd position: "+al.get(3));  
for(String s:al){  
 System.out.println(s);  
}  
}  
}  
```
After running this program , we can see that Output:
![3.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516457377/sliulaeaal38zfwdvpyk.png)

#### HashSet : Java HashSet class is used to create a collection that uses a hash table for storage.
Here , We can see an example of the Hashset :
```
import java.util.*;  
class hashset{  
 public static void main(String args[]){  
  //Creating HashSet and adding elements  
  HashSet<String> set=new HashSet<String>();  
  set.add("kamal ");  
  set.add("salim");  
  set.add("reza");  
  set.add("jafor");  
  //Traversing elements  
  Iterator<String> itr=set.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
```
Output :
![4.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516458165/to4sfimgl7fk4uhf1wfi.png)

#### Java TreeSet: Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. It contains Unique elements only like Hashset.
Here we can see an example of Java TreeSet -
```
import java.util.*;  
class treelist{  
 public static void main(String args[]){  
  //Creating and adding elements  
  TreeSet<String> al=new TreeSet<String>();  
  al.add("anwar");  
  al.add("jamal");  
  al.add("rafi");  
  al.add("mostak");  
  //Traversing elements  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  
```
Output : After running the program , we see the following Output :
![5.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516459064/tzyjhjfjodoapjwtfxqx.png)

#### Java Map Interface : A map contains values on the basis of key and value pair. Each key and value pair is known as an entry. Map contains only unique keys. Map Interface normally uses in search , Insert,Update and delete.
Let's see an example of Java Map Interface-
```
import java.util.*;  
class mapinterface{  
 public static void main(String args[]){  
  Map<Integer,String> map=new HashMap<Integer,String>();  
  map.put(1,"kamal");  
  map.put(2,"rahul");  
  map.put(3,"rafi");  
  for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}  
```
Output : After the running proram ,we can see the following Output :
![6.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516460443/wsfiarnlg65cvf80xqub.png)

#### Java Queue Interface : Java Queue interface orders the element in FIFO. In FIFO, first element is removed first and last element is removed at last. 

Example : 
```
import java.util.*;  
class queue{  
public static void main(String args[]){  
PriorityQueue<String> queue=new PriorityQueue<String>();  
queue.add("wahid");  
queue.add("kamal");  
queue.add("raafi");  
queue.add("joy");  
queue.add("rahul");  
System.out.println("head:"+queue.element());  
System.out.println("head:"+queue.peek());  
System.out.println("iterating the queue elements:");  
Iterator itr=queue.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
queue.remove();  
queue.poll();  
System.out.println("after removing two elements:");  
Iterator<String> itr2=queue.iterator();  
while(itr2.hasNext()){  
System.out.println(itr2.next());  
}  
}  
}  
```
OutPut :
![7.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516461665/ijhhaqmlgk8pbyvncly6.png)

#### Sorting in Collections : 
Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet. But We cannot sort the elements of List.
Let's see an example of sorting in collections :
```
import java.util.*;  
class sorting{  
public static void main(String args[]){  
  
ArrayList<String> al=new ArrayList<String>();  
al.add("wahid");  
al.add("kamal");  
al.add("sharif");  
al.add("arshad");  
  
Collections.sort(al);  
Iterator itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
 }  
}  
}  
```
Output :
![8.PNG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516463162/cxy6updjv2pirwdltnf2.png)


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@wahidurrahman/java-collections">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , , , , , , , ,