retainAll메서드 시그니처
boolean retainAll(Collection<?>c)
c에 있는 요소만 남깁니다.true, 그렇지 않으면 false를 반환합니다.List<Integer>list1=newArrayList<>(List.of(1,2,3,4,5));
List<Integer>list2=List.of(2,4,6);
list1.retainAll(list2);
System.out.println(list1); // [2, 4]
list2에 있는 값만 남았습니다.
Set<String>set1=newHashSet<>(Set.of("A","B","C"));
Set<String>set2=Set.of("B","C","D");
set1.retainAll(set2);
System.out.println(set1); // [B, C]
removeAll()과 비교list.retainAll(other);
↓
교집합
현재: [1,2,3,4]
상대: [3,4,5]
결과: [3,4]