Java LinkedHashSet contains()方法及示例

Java LinkedHashSet contains()方法及示例

在Java中,LinkedHashSet类包含的方法被称为contains() ,如果这个集合包含指定的元素,则返回真,否则返回假。

语法

public boolean contains(Object o)

参数: 元素 o 作为一个参数,其在这个集合中的存在要被测试。

返回类型: 一个布尔值,如果这个集合包含指定的元素,则 为真

例1 :

// Java program to Illustrate contains() Method
// of LinkedHashSet class
// For String value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty LinkedHashSet
            // Declaring object of string type
            LinkedHashSet<String> linkset
                = new LinkedHashSet<String>();
 
            // Populating above HashSet
            linkset.add("A");
            linkset.add("B");
            linkset.add("C");
 
            // Printing all elements of above LinkedHashSet
            System.out.println("LinkedHashSet: " + linkset);
 
            // Checking the existence of element
            // using contains() method
            boolean exist = linkset.contains("C");
 
            // Printing boolean value if present or not
            System.out.println("Is the element"
                               + " 'C' present: " + exist);
        }
 
        // Catch block to check for exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出

LinkedHashSet: [A, B, C]
Is the element 'C' present: true

例2 :

// Java program to Illustrate contains() Method
// of LinkedHashSet class
// For Integer value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty LinkedHashSet
            // Declaring object of integer type
            LinkedHashSet<Integer> linkset
                = new LinkedHashSet<Integer>();
 
            // Populating above HashSet
            linkset.add(10);
            linkset.add(20);
            linkset.add(30);
 
            // Printing all elements of above LinkedHashSet
            System.out.println("LinkedHashSet: " + linkset);
 
            // Checking the existence of element
            // using contains() method
            boolean exist = linkset.contains(25);
 
            // Printing boolean value if present or not
            System.out.println("Is the element"
                               + " '25' present: " + exist);
        }
 
        // Catch block to check for exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出

LinkedHashSet: [10, 20, 30]
Is the element '25' present: false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程