Java Program that Shows Use of Collection Interface

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:
- List Interface
- Linked List
- Map InterfaceÂ
- 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 Collectionpublic 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();Â
        // Condition 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();Â
        // Condition 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();Â
        // Condition check over elements inside using        // hasNext() method which holds true till there is        // element inside list        while (itr.hasNext()) {Â
            // Print all popped elements            System.out.println(itr.next());        }    }} |
Output
Geeks are you working



