####################################做个笔记##################################
一、java8的新特性,利用流的distinct来去重
public static List<String> deleteRepeat(List<String> list) {
List<String> myList = list.stream().distinct().collect(Collectors.toList());
return myList ;
}
二、Set集合的特性去重且保持循序TreeSet、LinkedHashSet
public static void deleteRepeat(List<String> list) {
//方法一
List<String> listNew = new ArrayList<String>(new TreeSet<String>(list));
//方法二
List<String> listNew2 = new ArrayList<String>(new LinkedHashSet<String>(list));
}
三、遍历集合,将元素加入另一个集合中
public static List<String> deleteRepeat(List<String> list) {
List<String> listNew = new ArrayList<String>();
for (String str : list) {
if (!listNew.contains(str)) {
listNew.add(str);
}
}
return listNew ;
}