Java AbstractSequentialList的contains()方法用於檢查Collection中是否存在元素。它使用元素作為參數,如果元素存在於集合中,則返回True。
用法:
public boolean contains(Object element)
參數:參數element 的類型為Collection。此參數表示需要在集合中檢查其出現的元素。
返回值:該方法返回一個布爾值。如果元素存在於Collection中,則返回True,否則返回False。
以下示例程序旨在說明AbstractSequentialList.contains()方法:
程序1:
// Java code to illustrate
// AbstractSequentialList.contains()
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Collection
AbstractSequentialList<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("AbstractSequentialList:"
+ 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"));
}
}
輸出:
AbstractSequentialList:[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 code to illustrate
// AbstractSequentialList.contains()
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Collection
AbstractSequentialList<Integer>
abs = new LinkedList<Integer>();
// Use add() method to add
// elements in the collection
abs.add(10);
abs.add(20);
abs.add(30);
abs.add(40);
abs.add(50);
// Displaying the collection
System.out.println("AbstractSequentialList:"
+ abs);
// Check if the collection contains 10
System.out.println("\nDoes the Collection "
+ "contains '10':"
+ abs.contains(10));
// Check if the collection contains 50
System.out.println("\nDoes the Collection"
+ " contains '50':"
+ abs.contains(50));
// Check if the Collection contains 100
System.out.println("Does the collection"
+ " contains '100':"
+ abs.contains(100));
}
}
輸出:
AbstractSequentialList:[10, 20, 30, 40, 50] Does the Collection contains '10':true Does the Collection contains '50':true Does the collection contains '100':false
相關用法
- Java AbstractSequentialList removeAll()用法及代碼示例
- Java AbstractSequentialList size()用法及代碼示例
- Java AbstractSequentialList isEmpty()用法及代碼示例
- Java AbstractSequentialList set()用法及代碼示例
- Java AbstractSequentialList toArray(T[])用法及代碼示例
- Java AbstractSequentialList get()用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java AbstractSequentialList toArray()用法及代碼示例
- Java AbstractSequentialList toString()用法及代碼示例
- Java AbstractSequentialList conatinsAll()用法及代碼示例
- Java AbstractSequentialList iterator()用法及代碼示例
- Java AbstractSequentialList clear()用法及代碼示例
- Java AbstractSequentialList set(int, Object)用法及代碼示例
- Java AbstractSequentialList equals()用法及代碼示例
- Java AbstractSequentialList indexOf()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 AbstractSequentialList contains() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。