상세 컨텐츠

본문 제목

[Java] 리스트의 중복을 제거하는 방법 - stream distinct

개발/Java

by 대충고양이짤 2022. 1. 10. 18:08

본문

distinct

stream distinct는 중복을 제거하는 공통 메소드이다. java 8 이상에서만 사용 가능

List<String> fruitList = Arrays.asList("사과", "배", "바나나", "자두", "딸기", "사과");
fruitList.stream().forEach(System.out::println);
System.out.println(" ------- distinct --------- ");
fruitList.stream().distinct().forEach(System.out::println);

새로운 변수에 저장하여 사용하자

List<String> result = fruitList.stream().distinct().collect(Collectors.toList());

결과

사과
배
바나나
자두
딸기
사과
 ------- distinct --------- 
사과
배
바나나
자두
딸기

관련글 더보기

댓글 영역