Java中的AbstractCollection contains()方法示例

Java中的AbstractCollection contains()方法示例

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

语法:

AbstractCollection.contains(Object element)
Java

参数: 参数 element 是Collection类型的。该参数指所需检查在集合中出现的元素。

返回值: 如果 element 在集合中出现,则该方法返回True,否则返回False。

以下程序演示了Java.util.AbstractCollection.contains()方法:

程序1:

// 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"));
    }
}
Java

输出:

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
Java

程序2:

// Java代码示例,演示布尔类型的contains()方法
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // 创建一个空的Collection
        AbstractCollection<String>
            abs = new TreeSet<String>();
 
        // 使用add()方法向Collection中添加元素
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("TreeSet");
        abs.add("20");
 
        // 显示Collection中的元素
        System.out.println("Abstract Collection:"
                           + abs);
 
        // 判断Collection是否包含"TreeSet"
        System.out.println("\nDoes the Collection "
                           + "contains 'TreeSet': "
                           + abs.contains("TreeSet"));
 
        // 判断Collection是否包含"Hello"
        System.out.println("\nDoes the Collection"
                           + " contains 'Hello': "
                           + abs.contains("Hello"));
 
        // 判断Collection是否包含"20"
        System.out.println("Does the collection"
                           + " contains '20': "
                           + abs.contains("20"));
 
        // 判断Collection是否包含"Geeks"
        System.out.println("Does the Collection"
                           + " contains 'Geeks': "
                           + abs.contains("Geeks"));
    }
}
Java

输出结果:

Abstract Collection:[20, Geeks, TreeSet, for]

是否包含'TreeSet'true

是否包含'Hello'false
是否包含'20'true
是否包含'Geeks'true
Java

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册