current position:Home>Two Set collections get the same elements

Two Set collections get the same elements

2022-08-06 14:32:55gentleman jiejie

Use retainAll() method, sample code:

 Set<String> set1 =span> new HashSet<>();set1.add("1");set1.add("2");set1.add("6");Set<String> set2 = newspan> HashSet<>();set2.add("2");set2.add("5");set2.add("6");set1.retainAll(set2);set1.forEach(item -> System.out.println(item));

The result is as follows:
insert image description here

copyright notice
author[gentleman jiejie],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/218/202208061424279251.html

Random recommended