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


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


java.util.Collection接口的contains(Object element)用于检查此集合中是否存在元素“ element”。此方法返回一个描述元素存在的布尔值。如果存在该元素,则返回true,否则返回false。

用法:

Collection.contains(Object element)

参数:此方法接受对象类型的必需参数element ,该元素将在此集合中进行检查。


返回值:此方法返回一个描述元素存在的布尔值。如果添加了元素,则返回true,否则返回false。

异常:此方法引发以下异常:

  • ClassCastException:如果指定元素的类阻止将其添加到此集合中
  • NullPointerException :如果指定的元素为null,并且此集合不允许使用null元素

以下示例说明了Collection contains()方法:

示例1:使用LinkedList类

// Java code to illustrate boolean contains() method 
  
import java.io.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
  
        // creating an empty LinkedList 
        Collection<String> list = new LinkedList<String>(); 
  
        // use add() method to add elements in the list 
        list.add("Geeks"); 
        list.add("for"); 
        list.add("Geeks"); 
  
        // Output the present list 
        System.out.println("The list is: " + list); 
  
        // Checking the presence of Geeks 
        // using contains() method 
        boolean result = list.contains("Geeks"); 
  
        // printing the result 
        System.out.println("Is Geeks present in the List: "
                           + result); 
    } 
}
输出:
The list is: [Geeks, for, Geeks]
Is Geeks present in the List: true

示例2:使用ArrayDeque类

// Java code to illustrate contains() method 
  
import java.util.*; 
  
public class ArrayDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ArrayDeque 
        Collection<String> de_que = new ArrayDeque<String>(); 
  
        // Use add() method to add elements into the Deque 
        de_que.add("Welcome"); 
        de_que.add("To"); 
        de_que.add("Geeks"); 
        de_que.add("4"); 
        de_que.add("Geeks"); 
  
        // Displaying the ArrayDeque 
        System.out.println("ArrayDeque: " + de_que); 
  
        // Checking the presence of Geeks 
        // using contains() method 
        boolean result = de_que.contains("Geeks"); 
  
        // printing the result 
        System.out.println("Is Geeks present in the ArrayDeque: "
                           + result); 
    } 
}
输出:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Is Geeks present in the ArrayDeque: true

示例3:使用ArrayList类

// Java code to illustrate contains() method 
  
import java.io.*; 
import java.util.*; 
  
public class ArrayListDemo { 
    public static void main(String[] args) 
    { 
  
        // create an empty array list with an initial capacity 
        Collection<Integer> arrlist = new ArrayList<Integer>(5); 
  
        // use add() method to add elements in the list 
        arrlist.add(15); 
        arrlist.add(20); 
        arrlist.add(25); 
  
        // Output the present list 
        System.out.println("ArrayList: " + arrlist); 
  
        // Checking the presence of 20 
        // using contains() method 
        boolean result = arrlist.contains(20); 
  
        // printing the result 
        System.out.println("Is 20 present in the ArrayList: "
                           + result); 
    } 
}
输出:
ArrayList: [15, 20, 25]
Is 20 present in the ArrayList: true

示例4:演示NullPointer异常

// Java code to illustrate boolean contains() 
  
import java.util.*; 
  
public class LinkedListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty ArrayList 
        Collection<String> 
            list = new ArrayList<String>(); 
  
        // Displaying the list 
        System.out.println("The ArrayList is: " + list); 
  
        try { 
            // Checking presence of null 
            list.contains(null); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
The ArrayList is: []

参考: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#contains-java.lang.Object-



相关用法


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