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
相關用法
- Java AbstractCollection clear()用法及代碼示例
- Java AbstractCollection addAll()用法及代碼示例
- Java AbstractCollection add()用法及代碼示例
- Java AbstractCollection isEmpty()用法及代碼示例
- Java AbstractCollection size()用法及代碼示例
- Java AbstractCollection remove()用法及代碼示例
- Java AbstractCollection containsAll()用法及代碼示例
- Java AbstractCollection toArray()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java AbstractCollection toString()用法及代碼示例
- Java AbstractCollection用法及代碼示例
- Java AbstractCollection removeAll()用法及代碼示例
- Java Guava Longs.contains()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java Collection contains()用法及代碼示例
- Java List contains()用法及代碼示例
- Java ConcurrentLinkedDeque contains()用法及代碼示例
- Java Set contains()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
- Java Guava Shorts.contains()用法及代碼示例
- Java Guava Bytes.contains()用法及代碼示例
- Java Guava Doubles.contains()用法及代碼示例
- Java Guava Floats.contains()用法及代碼示例
- Java Guava Chars.contains()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractCollection contains() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。