Skip to content
Related Articles

Related Articles

Improve Article
Java Program that Shows Use of Collection Interface
  • Last Updated : 22 Jun, 2021

The Collection framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.

Uses and advantages of Collection Framework:

  • This reduces the efforts of programmers by providing data structures and algorithms, so we do not have to write them.
  • This increases performance by providing a high-performance implementation of data structures and algorithms.
  • This provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.
  • Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc collection APIs.
  • This provides static methods that perform useful functions on collections, such as sorting a list.
  • This provides Wrapper implementation which adds functionality, such as synchronization, to other implementations.

And many more advantages it provides for us to use and tackle the problems of development.

Usages: Examples of different types of Interfaces are given below:

  1. List Interface
  2. Liked List
  3. Map Interface 
  4. Stacks

Use Case 1: List Interface



Java




// Java Program that Shows Use of Collection Interface
// ArrayList
 
import java.util.*;
 
class GFG {
    public static void main(String args[])
    {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Geeks");
        list.add("areyou");
        list.add("working");
        list.add("hard?");
 
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

 
 

Output
Geeks
areyou
working
hard?

 

Use Case 2: Linked List

 

Java




// Java Program that Shows Use of Collection Interface
// LinkedList
 
import java.util.*;
 
// Class testing java Collection
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating a LinkedList
        LinkedList<String> al = new LinkedList<String>();
 
        // Adding elements to above linked list
        al.add("Geeks");
        al.add("areyou");
        al.add("working");
        al.add("hard?");
 
        // Iterator
        Iterator<String> itr = al.iterator();
 
        // Conditin check over elements inside using
        // hasNext() method which holds true till there is
        // element inside list
        while (itr.hasNext()) {
 
            // Printing elements of LinkedList
            System.out.println(itr.next());
        }
    }
}

 
 

Output
Geeks
areyou
working
hard?

 



Use Case 3: Map Interface

 

Java




// Java Program that Shows Use of Collection Interface
// Hash-Map
 
import java.util.*;
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a Map
        Map<Integer, String> map = new HashMap<>();
 
        // Adding elements to map
        map.put(1, "Geeks");
        map.put(2, "are");
        map.put(3, "you");
        map.put(4, "working");
 
        // Traversing Map
        // Converting to Set so that traversal is accessed
        Set set = map.entrySet();
 
        // Iterator
        Iterator itr = set.iterator();
 
        // Conditin check over elements inside using
        // hasNext() method which holds true till there is
        // element inside list
 
        while (itr.hasNext()) {
            // Converting to Map.Entry so that we can get
            // key and value separately
 
            Map.Entry entry = (Map.Entry)itr.next();
 
            // Printing elements inside HashMap
            System.out.println(entry.getKey() + " "
                               + entry.getValue());
        }
    }
}

 
 

Output
1 Geeks
2 are
3 you
4 working

 

Use Case 4: Stacks

 

Java




// Java Program that Shows Use of Collection Interface
// Stack
 
import java.util.*;
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating a stack in memory
        Stack<String> stack = new Stack<String>();
 
        // Adding elements to stack
        stack.push("Geeks");
        stack.push("are");
        stack.push("you");
        stack.push("working");
        stack.push("hard?");
 
        // pop() returns all elements of stack
        stack.pop();
 
        //. iterator
        Iterator<String> itr = stack.iterator();
 
        // Conditin check over elements inside using
        // hasNext() method which holds true till there is
        // element inside list
        while (itr.hasNext()) {
 
            // Print all popped elemments
            System.out.println(itr.next());
        }
    }
}

 
 

Output
Geeks
are
you
working

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :