Java.util.TreeSet.isEmpty()方法用于检查和验证TreeSet是否为空。如果TreeSet为空,则返回True,否则返回False。

句法:

TreeSet.isEmpty()

参数:该方法不带任何参数

返回值:如果set为空,则函数返回True,否则返回False。

下面的程序说明了Java.util.TreeSet.isEmpty()方法的用法:

// Java code to illustrate isEmpty() method
import java.util.*;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty HashSet
        TreeSet<String> tree = new TreeSet<String>();

        // Use add() method to add elements into the Set
        tree.add("Welcome");
        tree.add("To");
        tree.add("Geeks");
        tree.add("4");
        tree.add("Geeks");
        tree.add("TreeSet");

        // Displaying the TreeSet
        System.out.println("TreeSet: " + tree);

        // Check for the empty set
        System.out.println("Is the set empty? " + tree.isEmpty());

        // Clearing the set using clear() method
        tree.clear();

        // Again Checking for the empty set
        System.out.println("Is the set empty? " + tree.isEmpty());
    }
}

输出:

TreeSet: [4, Geeks, To, TreeSet, Welcome]
Is the set empty? false
Is the set empty? true

 
转载请保留页面地址:https://www.breakyizhan.com/java/4927.html
扫描二维码添加微信 
  • ,每次淘宝领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘50元优惠券!添加微信后回复机械键盘即可领取!
    支持我们,就用微信淘宝!