ホームページ  >  記事  >  Java  >  Java のコレクション インターフェイス

Java のコレクション インターフェイス

WBOY
WBOYオリジナル
2024-08-30 16:04:41615ブラウズ

コレクション インターフェイスは、コレクション フレームワークの基本的なルート インターフェイスであり、コレクション インターフェイスのすべてのメンバーが使用する必要がある基本メソッドを定義します。データの処理、操作、アクセスのさまざまな方法を含むコレクション フレームワークは、このコレクション インターフェイスに基づいています。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java のコレクション インターフェイス

Java のコレクション インターフェイスは java.util.Collection パッケージで使用でき、コレクション ファミリのすべてのメンバーが実装する必要がある基本メソッドを定義します。

次のメソッドはコレクション インターフェイスで定義されており、すべてのコレクション フレームワークで実装する必要があります。

Method Name Method Description
boolean add(Object object) This method adds the specified object to the collection calling this method. It returns true if the object was added to the collection otherwise returns false if the object is already a part of the collection or if the collection does not allow duplicates elements.
boolean addAll(Collection collection) This method adds all the elements of the specified collection to the collection calling this method. It returns true if the addition is successful, otherwise, it returns false.
boolean contains(Object object) This method returns true if the specified object is a part of the collection calling this method; otherwise, it returns false.
void clear( ) This method removes all elements from the collection calling this method.
boolean containsAll(Collection collection) This method returns true if the collection calling this method contains all specified collection elements; otherwise, it returns false.
boolean equals(Object object) This method returns true if the collection calling this method and the specified object are equal; otherwise, it returns false.
int hashCode( ) This method returns the hash code for the collection calling this method.
boolean isEmpty( ) This method returns true if the collection calling this method contains no elements; otherwise, it returns false.
Iterator iterator( ) This method returns an iterator for the collection calling this method.
boolean remove(Object object) This method removes the specified object from the collection calling this method. Returns true if the specified object was removed, otherwise returns false.
boolean removeAll(Collection collection) This method removes all elements available in a specified collection from the collection calling this method. Returns true if there is a change in collection calling this method; otherwise, it returns false.
boolean retainAll(Collection collection) This method removes all elements from the collection calling this method except those available in the specified collection. This method returns true if there is a change in collection calling this method; otherwise, it returns false.
int size( ) This method returns the number of elements available in the collection calling this method.
Object[ ] toArray( ) This method returns an array that contains all the elements stored in the collection calling this method. The array elements are, in fact, copies of the elements available in the calling collection.
Object[ ] toArray(Object array[ ]) This method returns an array containing only those collection elements of the calling collection whose type matches the type of elements in the specified array.

Java のコレクション インターフェースの例

以下は Java のコレクション インターフェースの例です。

例 #1

この例では、Java のコレクション インターフェイスのメソッドを示します。 add、addAll、size メソッドの使用方法を見ていきます。

コード:

package com.edubca.nonprimitivedemo;
// import collections
import java.util.ArrayList;
import java.util.List;
public class DataTypeDemo {
public static void main(String[] args) {
List arraylist1 = new ArrayList();
// Adding elements to arraylist
arraylist1.add("Yash");
arraylist1.add("Montu");
arraylist1.add("Ketan");
System.out.println(" ArrayList1 Elements are :");
System.out.println("\t" + arraylist1);
//printing size of arraylist1
System.out.println("Size of ArrayList1 is " +arraylist1.size() );
List arraylist2 = new ArrayList();
// Adding elements to arraylist
arraylist2.add("Chetan");
arraylist2.add("Chirag");
arraylist2.add("Ali");
System.out.println();
System.out.println(" ArrayList2 Elements are :");
System.out.println("\t" + arraylist2);
//printing size of arraylist2
System.out.println("Size of ArrayList1 is " +arraylist2.size() );
System.out.println();
// Adding elements of both lists to list1
arraylist1.addAll(arraylist2);
// Now Printing modified list
System.out.println(" ArrayList1 Elements after merging with ArrayList2 are : ");
System.out.println("\t" + arraylist1);
//printing size of arraylist1
System.out.println("Size of ArrayList1 is " +arraylist1.size() );
}
}

出力:

Java のコレクション インターフェイス

例 #2

この例では、コレクション インターフェイスのメソッドをさらにいくつか見ていきます。

コード:

package com.edubca.nonprimitivedemo;
// import collections
import java.util.ArrayList;
import java.util.List;
// importing iterator
import java.util.Iterator;
public class DataTypeDemo {
public static void main(String[] args) {
// creating array list of type string
ArrayList<String> arraylist1 = new ArrayList<String>();
arraylist1.add("Spark");
arraylist1.add("Hadoop");
arraylist1.add("Hive");
arraylist1.add("Kafka");
System.out.println("Elements of list1 are : " + arraylist1);
ArrayList<String> arraylist2 = new ArrayList<String>();
arraylist2.add("Spark");
arraylist2.add("Hadoop");
arraylist2.add("Hive");
arraylist2.add("Kafka");
boolean isequal=arraylist1.equals(arraylist2); // check if both lists are equal
System.out.println("Elements of list2 are : " + arraylist2);
System.out.println ("Are Both Lists equal? " + isequal);
arraylist1.addAll(arraylist2); // merging both lists
// iterating arraylist
Iterator it=arraylist1.iterator();
System.out.println("Iterating Elements of Arraylist:");
while(it.hasNext()){
System.out.println(it.next());
}
}
}

出力:

Java のコレクション インターフェイス

結論

上記の記事では、Java のコレクション インターフェイスと、コレクション インターフェイスで使用できるさまざまなメソッドについて明確に理解できます。上記の Java コード例は、コレクション ファミリのメンバーがコレクション インターフェイスのメソッドをどのように使用するかを示しています。

以上がJava のコレクション インターフェイスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:JavaのPriorityQueue次の記事:JavaのPriorityQueue