import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class RemoveListduplicate {
public static void main(String[] args) {
List<String> list = Arrays.asList("a","b","c","a","b","c");
method1(list);//将List集合转换为Set集合
method2(list);//JDK8的流式编程
}
private static void method1(List<String> list) {
Set<String> set = new HashSet<>();
for (String word:list) {
set.add(word);
}
for (String word:set) {
System.out.print(word+" ");
}
System.out.println();
System.out.println("===我是分隔符===");
}
private static void method2(List<String> list) {
list.stream().distinct().collect(Collectors.toList()).forEach(System.out::print);//冒号为方法引用
}
}
在Java中如何将List中的元素去重
最新推荐文章于 2024-07-23 20:07:32 发布