> Java > java지도 시간 > 본문

Java의 ArrayList에서 중복 요소를 삭제하는 2가지 방법

高洛峰
풀어 주다: 2017-01-22 16:08:25
원래의
1328명이 탐색했습니다.

이 기사에서는 HashSet 및 LinkedHashSet을 사용하여 ArrayList에서 중복 요소를 제거하는 두 가지 방법을 제공합니다.

ArrayList는 Java에서 가장 일반적으로 사용되는 컬렉션 유형 중 하나입니다. 여러 null 요소, 중복 요소를 유연하게 추가하고 요소 삽입 순서를 유지 관리할 수 있습니다. 코딩할 때, 구축된 ArrayList에서 중복 요소를 제거해야 한다는 요구 사항이 종종 발생합니다.

방법 1: HashSet을 사용하여 ArrayList의 중복 요소를 삭제합니다.

이 방법에서는 HashSet을 사용하여 중복 요소를 삭제합니다. 아시다시피 HashSet은 중복 요소를 허용하지 않습니다. 우리는 HashSet의 이 속성을 사용하여 빌드된 ArrayList에서 중복된 요소를 삭제합니다. 그러나 이 접근 방식에는 단점이 있습니다. 즉, ArrayList의 요소 삽입 순서를 제거합니다. 이는 중복 요소를 제거한 후 요소가 잘못된 순서로 삽입됨을 의미합니다. 먼저 다음 예를 살펴보겠습니다.

import java.util.ArrayList;
import java.util.HashSet;
  
public class MainClass
{
 public static void main(String[] args)
 {
 //Constructing An ArrayList
  
 ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("J2EE");
  
 listWithDuplicateElements.add("JSP");
  
 listWithDuplicateElements.add("SERVLETS");
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("STRUTS");
  
 listWithDuplicateElements.add("JSP");
  
 //Printing listWithDuplicateElements
  
 System.out.print("ArrayList With Duplicate Elements :");
  
 System.out.println(listWithDuplicateElements);
  
 //Constructing HashSet using listWithDuplicateElements
  
 HashSet<String> set = new HashSet<String>(listWithDuplicateElements);
  
 //Constructing listWithoutDuplicateElements using set
  
 ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  
 //Printing listWithoutDuplicateElements
  
 System.out.print("ArrayList After Removing Duplicate Elements :");
  
 System.out.println(listWithoutDuplicateElements);
 }
}
로그인 후 복사


출력:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]
로그인 후 복사

출력에 주의하세요. 중복된 요소를 제거한 후 요소가 다시 섞이는 것을 볼 수 있습니다. 더 이상 삽입 순서로 정렬되지 않습니다. 중복된 요소를 제거한 후 요소의 삽입 순서를 유지하려는 경우에는 이 방법을 권장하지 않습니다. 중복된 요소를 제거한 후 요소의 삽입 순서가 변경되지 않도록 하는 또 다른 방법이 있습니다. LinkedHashSet을 사용하는 것입니다.

방법 2: LinkedHashSet을 사용하여 ArrayList의 중복 요소를 삭제합니다.

이 방법에서는 LinkedHashSet을 사용하여 ArrayList의 중복 요소를 삭제합니다. 아시다시피 LinkedHashSet은 요소 삽입 순서를 유지하면서 중복 요소를 허용하지 않습니다. LinkedHashSet의 이 두 가지 속성은 ArrayList에서 중복 요소를 삭제한 후에도 요소의 삽입 순서가 계속 유지되도록 보장할 수 있습니다. 아래 예를 참조하세요.

import java.util.ArrayList;
import java.util.LinkedHashSet;
  
public class MainClass
{
 public static void main(String[] args)
 {
 //Constructing An ArrayList
  
 ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("J2EE");
  
 listWithDuplicateElements.add("JSP");
  
 listWithDuplicateElements.add("SERVLETS");
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("STRUTS");
  
 listWithDuplicateElements.add("JSP");
  
 //Printing listWithDuplicateElements
  
 System.out.print("ArrayList With Duplicate Elements :");
  
 System.out.println(listWithDuplicateElements);
  
 //Constructing LinkedHashSet using listWithDuplicateElements
  
 LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);
  
 //Constructing listWithoutDuplicateElements using set
  
 ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  
 //Printing listWithoutDuplicateElements
  
 System.out.print("ArrayList After Removing Duplicate Elements :");
  
 System.out.println(listWithoutDuplicateElements);
 }
}
로그인 후 복사


출력:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]
로그인 후 복사

출력에 주의하세요. ArrayList에서 중복된 요소를 삭제한 후에도 요소의 삽입 순서가 계속 유지되는 것을 확인할 수 있습니다.

위 내용은 이 글의 전체 내용이므로, 모든 분들의 공부에 도움이 되었으면 좋겠습니다.

ArrayList의 중복 요소를 삭제하는 2가지 이상의 Java 메소드에 대한 관련 기사는 PHP 중국어 웹사이트를 참고하세요!

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