> Java > java지도 시간 > 본문

Java의 컬렉션 인터페이스

WBOY
풀어 주다: 2024-08-30 16:04:41
원래의
609명이 탐색했습니다.

컬렉션 인터페이스는 컬렉션 프레임워크의 기본 루트 인터페이스이며 컬렉션 인터페이스의 모든 구성원이 사용해야 하는 기본 메서드를 정의합니다. 데이터를 처리, 조작 및 액세스하는 다양한 방법을 포함하는 컬렉션 프레임워크는 이 컬렉션 인터페이스를 기반으로 합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!