📜  显示集合接口使用的Java程序

📅  最后修改于: 2022-05-13 01:54:42.785000             🧑  作者: Mango

显示集合接口使用的Java程序

Collection 框架是用于表示和操作集合的统一架构,使集合能够独立于实现细节进行操作。

集合框架的用途和优势:

  • 这通过提供数据结构和算法减少了程序员的工作,因此我们不必编写它们。
  • 这通过提供数据结构和算法的高性能实现来提高性能。
  • 这通过建立一种公共语言来来回传递集合,从而在不相关的 API 之间提供互操作性。
  • 通过要求您学习多个临时集合 API 来减少学习 API 所需的工作量。
  • 这提供了对集合执行有用功能的静态方法,例如对列表进行排序。
  • 这提供了 Wrapper 实现,它向其他实现添加了功能,例如同步。

它为我们提供了使用和解决发展问题的更多优势。

用法:下面给出了不同类型接口的示例:

  1. 列表界面
  2. 喜欢的列表
  3. 地图界面
  4. 堆栈

用例 1:列表接口

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


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 al = new LinkedList();
 
        // Adding elements to above linked list
        al.add("Geeks");
        al.add("areyou");
        al.add("working");
        al.add("hard?");
 
        // Iterator
        Iterator 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());
        }
    }
}


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 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());
        }
    }
}


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 stack = new Stack();
 
        // 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 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());
        }
    }
}


输出
Geeks
areyou
working
hard?

用例 2:链表

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 al = new LinkedList();
 
        // Adding elements to above linked list
        al.add("Geeks");
        al.add("areyou");
        al.add("working");
        al.add("hard?");
 
        // Iterator
        Iterator 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());
        }
    }
}
输出
Geeks
areyou
working
hard?

用例 3:地图界面

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 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());
        }
    }
}
输出
1 Geeks
2 are
3 you
4 working

用例 4:堆栈

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 stack = new Stack();
 
        // 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 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());
        }
    }
}
输出
Geeks
are
you
working