###删除List集合中的重复元素
package com.mypro.test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Created by Administrator on 2018/6/21.
*/
public class DelRepeatObj {
public static void main(String[] agrs){
List list = new ArrayList();
list.add("aaa");
list.add("aaa");
list.add("bbb");
list.add("bbb");
list.add("ccc");
list.add("eee");
list.add("fff");
list.add("eee");
// System.out.println(getNewList(list));
getNewListNoRepeat(list);
}
/**
* 去除List集合中的重复元素
*/
public static Set getNewList(List<String> list){
//将集合存入set集合中
Set set = new HashSet();
set.addAll(list);
//jdk1.8+ 利用Stream API去重
// Stream distinct = list.stream().distinct();
// Object collect = distinct.collect(Collectors.toSet());
return set;
}
/**
* 取出List集合中的重复元素
*/
public static List<String> getNewListNoRepeat(List<String> list){
List<String> strings = list.stream()
.map(e -> {
return e.toString();
}).collect(Collectors.toMap(e -> e, e -> 1, Integer::sum))
.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
return strings;
}
}