Java SortedSet isEmpty()方法及示例
java.util.SortedSet .isEmpty() 方法是用来检查一个排序集是否为空。如果SortedSet是空的,它返回True,否则返回False。
语法
boolean isEmpty()
参数: 该方法不接受任何参数。
返回值: 如果SortedSet是空的,该方法返回True,否则返回False。
注意 :SortedSet中的isEmpty()方法是继承自Java中的Set接口。
下面的程序说明了java.util.SortedSet.isEmpty()方法。
// Java code to illustrate isEmpty() import java.io.*;import java.util.*; public class SortedSetDemo { public static void main(String args[]) { // Creating an empty Set SortedSet<String> set = new TreeSet<String>(); // Use add() method to // add elements into the Set set.add("Welcome"); set.add("To"); set.add("Geeks"); set.add("4"); set.add("Geeks"); // Displaying the Set System.out.println("Set: " + set); // Check for the empty set System.out.println("Is the set empty? " + set.isEmpty()); // Clearing the set using clear() method set.clear(); // Again Checking for the empty set System.out.println("Is the set empty? " + set.isEmpty()); }}输出:
Set: [4, Geeks, To, Welcome]Is the set empty? falseIs the set empty? true
参考资料 : https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#isEmpty()
