Java.util.TreeSet.contains()方法用于检查TreeSet中是否存在特定元素。所以基本上它用于检查TreeSet是否包含任何特定元素。

句法:

Tree_Set.contains(Object element)

参数:参数元素属于TreeSet类型。如果它存在于TreeSet中,则需要检查该元素。

返回值:如果元素存在于集合中,则该方法返回true,否则返回False。

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

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

public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        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 "TreeSet" in the set
        System.out.println("Does the Set contains 'TreeSet'? " + 
                                           tree.contains("TreeSet"));

        // Check for "4" in the set
        System.out.println("Does the Set contains '4'? " + 
                                             tree.contains("4"));

        // Check if the list contains "No"
        System.out.println("Does the Set contains 'No'? " + 
                                             tree.contains("No"));
    }
}

输出:

TreeSet: [4, Geeks, To, TreeSet, Welcome]
Does the Set contains 'TreeSet'? true
Does the Set contains '4'? true
Does the Set contains 'No'? false
 
转载请保留页面地址:https://www.breakyizhan.com/java/4921.html
扫描二维码添加微信 
  • ,每次淘宝领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘50元优惠券!添加微信后回复机械键盘即可领取!
    支持我们,就用微信淘宝!