Ad Code

Collection Framework in Java.

Collection Framework 

Hy, In this tutorial, we will discuss Collection Framework in a java programming language.

Collection Framework is a collection of interfaces and classes. It is very useful for a programmer. Java Collections can achieve all the operations that you perform on data such as searching, sorting, insertion, manipulation, and deletion.

How to Import Java Collection:

 import java.util.*;

Collection Framework

Interfaces:-

> List:

List interface is the child interface of the Collection interface. It inhibits a list-type data structure in which we can store the ordered collection of objects. It can have duplicate values.
 List <data-type> l1= new ArrayList();  
 List <data-type> l2 = new LinkedList();  
 List <data-type> l3 = new Vector();  
 List <data-type> l4 = new Stack();

> Set:

Set Interface in Java is present in java.util package. It extends the Collection interface. It 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>();

> Queue: 

Queue interface maintains the first-in-first-out order. It can be defined as an ordered list that is used to hold the elements which are about to be processed. There are various classes like PriorityQueue, Deque, and ArrayDeque which implements the Queue interface.
 Queue <String> q1 = new PriorityQueue();  
 Queue <String> q2 = new ArrayDeque();

Classes:-

> ArrayList:

The ArrayList class implements the List interface.
 import java.util.*;  
 public class arraylist
    {  
    public static void main(String args[])
    {  
        ArrayList<String> list=new ArrayList<String>(); 
        list.add("Ravi");  
        list.add("Vijay");  
        list.add("Ravi");  
        list.add("Ajay");  
        System.out.println(list);
    }  
 }  

Output:

Ravi

Vijay

Ravi

Ajay

> ListedList:

The LinkedList class implements the List interface.
 import java.util.*;  
 public class Linkedlist
    {  
    public static void main(String args[])
    {  
        LinkedList<String> list=new LinkedList<String>();  
        list.add("Ravi"); 
        list.add("Vijay");  
        list.add("Ravi");  
        list.add("Ajay");  
        System.out.println(list);
    }  
 }  

Output:

Ravi

Vijay

Ravi

Ajay


Vector:

The Vector class implements the List interface.
 import java.util.*;  
 public class Vector
    {  
    public static void main(String args[])
    {  
    Vector<String> list=new Vector<String>();
        list.add("Ravi");
        list.add("Vijay");  
        list.add("Ravi");  
        list.add("Ajay");  
        System.out.println(list);
    }  
 }  

Output:

Ravi

Vijay

Ravi

Ajay


> PriorityQueue:

The PriorityQueue class implements the Queue interface.
 import java.util.*;  
 public class priorityqueue
    {  
    public static void main(String args[])
    {  
        PriorityQueue<String> queue=new PriorityQueue<String>();
        queue.add("Ravi");//Adding object in arraylist  
        queue.add("Vijay");  
        queue.add("Ravi");  
        queue.add("Ajay");  
        System.out.println(queue);
    }  
 }  

Output:

Ravi

Vijay

Ravi

Ajay


> HashSet:

The HashSet class implements the Set interface. HashSet contains unique elements.

 import java.util.*;  
 public class hashset{  
    public static void main(String args[])
    {  
        HashSet<String> set=new HashSet<String>(); 
        set.add("Ravi");//Adding object in arraylist  
        set.add("Vijay");  
        set.add("Ravi");  
        set.add("Ajay");  
        System.out.println(set);
    }  
 }  

Output:

Vijay

Ravi

Ajay

> TreeSet:

The TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet also contains unique elements.
 import java.util.*;  
 public class hashset{  
    public static void main(String args[])
    {  
        TreeSet<String> set=new TreeSet<String>(); 
        set.add("Ravi");//Adding object in arraylist  
        set.add("Vijay");  
        set.add("Ravi");  
        set.add("Ajay");  
        System.out.println(set);
    }  
 }  

Output:

Ajay
Ravi
Vijay

> HashTable:

The HashTable class implements the Set interface which maps keys to values.

 import java.util.*; 
 public class hashexample {
    public static void main(String args[])
    {
        Hashtable<Integer, String> ht1 = new Hashtable<>();
        Hashtable<Integer, String> ht2 = new Hashtable<Integer, String>();
        ht1.put(1, "one");
        ht1.put(2, "two");
        ht1.put(3, "three");
        ht2.put(4, "four");
        ht2.put(5, "five");
        ht2.put(6, "six");
        System.out.println(ht1);
        System.out.println(ht2);
    }
 }

 Output:

{3=three, 2=two, 1=one}

{6=six, 5=five, 4=four}


Post a Comment

0 Comments

Ad Code