📜  Java的集合与集合的示例

📅  最后修改于: 2021-09-16 10:20:20             🧑  作者: Mango

集合:集合是Java.util.package 中的一个接口。它用于将一组单个对象表示为一个单元。它类似于 C++ 语言中的容器。集合被认为是集合框架的根接口。它提供了几个类和接口来将一组单独的对象表示为一个单元。

List、Set 和 Queue 是集合接口的主要子接口。 map接口也是Java集合框架的一部分,但是不继承接口的集合。 add()remove()clear()size()contains()是 Collection 接口的重要方法。

宣言:

public interface Collection extends Iterable

Type Parameters: E - the type of elements returned by this iterator

集合:集合是Java.util.package 中的一个实用程序类。它定义了几种实用方法,例如排序和搜索,用于对集合进行操作。它具有所有静态方法。这些方法为开发人员提供了急需的便利,使他们能够有效地使用 Collection Framework。例如,它有一个方法sort()根据默认排序顺序对集合元素进行排序,它有一个方法min()max()分别在集合元素中查找最小值和最大值。

宣言:

public class Collections extends Object

收藏与收藏:                                                           

                                  Collection                                           Collections
It is an interface. It is a utility class.
It is used to represent a group of individual objects as a single unit.  It defines several utility methods that are used to operate on collection.
The Collection is an interface that contains a static method since java8. The Interface can also contain abstract and default methods. It contains only static methods.
Java
// Java program to demonstrate the difference  
// between Collection and Collections
  
import java.io.*;
import java.util.*;
  
class GFG {
    
    public static void main (String[] args) 
    {
        
      // Creating an object of List
      List arrlist = new ArrayList(); 
        
      // Adding elements to arrlist
      arrlist.add("geeks");
      arrlist.add("for");
      arrlist.add("geeks");
        
      // Printing the elements of arrlist
      // before operations
      System.out.println("Elements of arrlist before the operations:");
      System.out.println(arrlist);
        
      System.out.println("Elements of arrlist after the operations:");
        
      // Adding all the specified elements
      // to the specified collection
      Collections.addAll(arrlist, "web", "site");
        
      // Printing the arrlist after
      // performing addAll() method
      System.out.println(arrlist);
        
      // Sorting all the elements of the  
      // specified collection according to 
      // default sorting order
      Collections.sort(arrlist);
          
      // Printing the arrlist after
      // performing sort() method
      System.out.println(arrlist);
          
    }
}


输出
Elements of arrlist before the operations:
[geeks, for, geeks]
Elements of arrlist after the operations:
[geeks, for, geeks, web, site]
[for, geeks, geeks, site, web]