当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AbstractCollection contains()用法及代码示例


Java AbstractCollection 的 contains() 方法用于检查集合中是否存在元素。它将元素作为参数,如果元素存在于集合中,则返回 True。语法:

AbstractCollection.contains(Object element)

参数:参数element 是 Collection 类型。该参数是指需要在集合中检查是否出现的元素。返回值:如果该元素存在于集合中,则该方法返回True,否则返回False。以下程序说明Java.util.AbstractCollection.contains()方法:方案一:

Java


// Java code to illustrate boolean contains()
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new LinkedList<String>();
        // Use add() method to add
        // elements in the collection
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("10");
        abs.add("20");
        // Displaying the collection
        System.out.println("Abstract Collection:"
                           + abs);
        // Check if the collection contains "Hello"
        System.out.println("\nDoes the Collection"
                           + " contains 'Hello':"
                           + abs.contains("Hello"));
        // Check if the Collection contains "20"
        System.out.println("Does the collection"
                           + " contains '20':"
                           + abs.contains("20"));
        // Check if the Collection contains "Geeks"
        System.out.println("Does the Collection"
                           + " contains 'Geeks':"
                           + abs.contains("Geeks"));
    }
}
输出:
Abstract Collection:[Geeks, for, Geeks, 10, 20]

Does the Collection contains 'Hello':false
Does the collection contains '20':true
Does the Collection contains 'Geeks':true

程序2:

Java


// Java code to illustrate boolean contains()
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new TreeSet<String>();
        // Use add() method to add
        // elements in the collection
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("TreeSet");
        abs.add("20");
        // Displaying the collection
        System.out.println("Abstract Collection:"
                           + abs);
        // Check if the collection contains "TreeSet"
        System.out.println("\nDoes the Collection "
                           + "contains 'TreeSet':"
                           + abs.contains("TreeSet"));
        // Check if the collection contains "Hello"
        System.out.println("\nDoes the Collection"
                           + " contains 'Hello':"
                           + abs.contains("Hello"));
        // Check if the Collection contains "20"
        System.out.println("Does the collection"
                           + " contains '20':"
                           + abs.contains("20"));
        // Check if the Collection contains "Geeks"
        System.out.println("Does the Collection"
                           + " contains 'Geeks':"
                           + abs.contains("Geeks"));
    }
}
输出:
Abstract Collection:[20, Geeks, TreeSet, for]

Does the Collection contains 'TreeSet':true

Does the Collection contains 'Hello':false
Does the collection contains '20':true
Does the Collection contains 'Geeks':true




相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 AbstractCollection contains() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。