📜  Java中的集合 isEmpty() 方法和示例

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

Java中的集合 isEmpty() 方法和示例

Java.util.Collection 接口isEmpty()用于检查调用它的 Collection 是否为空。该方法不带任何参数,也不返回任何值。

句法:

Collection.isEmpty()

参数:此方法不接受任何参数

返回值:此方法不返回任何值。

下面的示例说明了 Collection isEmpty() 方法:

示例 1:使用 LinkedList 类

// Java code to illustrate boolean isEmpty() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        // creating an empty LinkedList
        Collection list = new LinkedList();
  
        // use add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
  
        // Output the present list
        System.out.println("The list is: " + list);
  
        // Check if list is empty
        // using isEmpty() method
        System.out.println("Is the LinkedList empty: "
                           + list.isEmpty());
  
        // Clearing the LinkedList
        list.clear();
  
        // printing the new list
        System.out.println("The new List is: " + list);
  
        // Check if list is empty
        // using isEmpty() method
        System.out.println("Is the LinkedList empty: "
                           + list.isEmpty());
    }
}
输出:
The list is: [Geeks, for, Geeks]
Is the LinkedList empty: false
The new List is: []
Is the LinkedList empty: true

示例 2:使用 ArrayDeque 类

// Java code to illustrate isEmpty() method
  
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Collection de_que = new ArrayDeque();
  
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
  
        // Check if ArrayDeque is empty
        // using isEmpty() method
        System.out.println("Is the ArrayDeque empty: "
                           + de_que.isEmpty());
  
        // Clearing the ArrayDeque
        de_que.clear();
  
        // printing the new ArrayDeque
        System.out.println("The new ArrayDeque is: "
                           + de_que);
  
        // Check if ArrayDeque is empty
        // using isEmpty() method
        System.out.println("Is the ArrayDeque empty: "
                           + de_que.isEmpty());
    }
}
输出:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Is the ArrayDeque empty: false
The new ArrayDeque is: []
Is the ArrayDeque empty: true

示例 3:使用 ArrayList 类

// Java code to illustrate isEmpty() method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // create an empty array list with an initial capacity
        Collection
            arrlist = new ArrayList(5);
  
        // use add() method to add elements in the list
        arrlist.add(15);
        arrlist.add(20);
        arrlist.add(25);
  
        // prints all the elements available in list
        System.out.println("ArrayList: " + arrlist);
  
        // Check if list is empty
        // using isEmpty() method
        System.out.println("Is the ArrayList empty: "
                           + arrlist.isEmpty());
  
        // Clearing the ArrayList
        arrlist.clear();
  
        // printing the new ArrayList
        System.out.println("The new ArrayList is: "
                           + arrlist);
  
        // Check if ArrayList is empty
        // using isEmpty() method
        System.out.println("Is the ArrayList empty: "
                           + arrlist.isEmpty());
    }
}
输出:
ArrayList: [15, 20, 25]
Is the ArrayList empty: false
The new ArrayList is: []
Is the ArrayList empty: true

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Collection.html#isEmpty–