📜  获取集合大小并验证集合为空的Java程序

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

获取集合大小并验证集合为空的Java程序

尺寸() Java.util.Collection 接口的isEmpty()用于检查集合的大小以及集合是否为空。 isEmpty()方法不接受任何参数,也不返回任何值。

例子:

Input:[99, 54, 112, 184, 2]
Output:size = 5 and Collection is not empty

Input:[]
Output: size = 0 and Collection is empty

大小()方法:

句法:

public int size()

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

返回值:此方法返回此列表中的元素数。

isEmpty() 方法:

句法:

Collection.isEmpty()

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

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

示例 1:使用 LinkedList 类

Java
//Java Program to Get the Size of Collection 
// and Verify that Collection is Empty
// using LinkedList class
  
import java.io.*;
import java.util.*; 
  
class GFG {
    public static void main (String[] args) {
        
        Collection list = new  LinkedList();  
        
        //add integer values in this list  
        list.add(99);  
        list.add(54);  
        list.add(112);
          list.add(184);
          list.add(2);
        
        // Output the present list 
        System.out.print("The elements in Collection : ");  
        System.out.println(list);  
        
        //returns the size of the list
        System.out.println("Size of the collection "+list.size()); 
        
        // 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()); 
    }
}


Java
//Java Program to Get the Size of Collection and 
// Verify that Collection is Empty 
// using ArrayList class
  
import java.io.*;
import java.util.*; 
  
class GFG {
    public static void main (String[] args) {
        
        Collection arraylist = new ArrayList();  
        
           arraylist.add("Geeks");  
        arraylist.add("for");  
        arraylist.add("geeks");
        
        // returns the size of the arraylist
        System.out.println("Size of the collection "+arraylist.size()); 
        
        // Check if list is empty using isEmpty() method 
        System.out.println("Is the ArrayList empty: "
                           + arraylist.isEmpty()); 
    }
}


输出
The elements in Collection : [99, 54, 112, 184, 2]
Size of the collection 5
Is the LinkedList empty: false
The new List is: []
Is the LinkedList empty: true

示例 2:使用 ArrayList 类

Java

//Java Program to Get the Size of Collection and 
// Verify that Collection is Empty 
// using ArrayList class
  
import java.io.*;
import java.util.*; 
  
class GFG {
    public static void main (String[] args) {
        
        Collection arraylist = new ArrayList();  
        
           arraylist.add("Geeks");  
        arraylist.add("for");  
        arraylist.add("geeks");
        
        // returns the size of the arraylist
        System.out.println("Size of the collection "+arraylist.size()); 
        
        // Check if list is empty using isEmpty() method 
        System.out.println("Is the ArrayList empty: "
                           + arraylist.isEmpty()); 
    }
}
输出
Size of the collection 3
Is the ArrayList empty: false