List集合去重
Posted move22
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List集合去重相关的知识,希望对你有一定的参考价值。
一、String去重
//set集合去重,不改变原有的顺序 public static void removeDuplicate1(List<String> list){ System.out.println("list = [" + list.toString() + "]"); List<String> listNew=new ArrayList<>(); Set set=new HashSet(); for (String str:list) { if(set.add(str)){ listNew.add(str); } } System.out.println("listNew = [" + listNew.toString() + "]"); } //遍历后判断赋给另一个list集合 public static void removeDuplicate2(List<String> list){ System.out.println("list = [" + list.toString() + "]"); List<String> listNew=new ArrayList<>(); for (String str:list) { if(!listNew.contains(str)){ listNew.add(str); } } System.out.println("listNew = [" + listNew.toString() + "]"); } //set去重 public static void removeDuplicate3(List<String> list){ System.out.println("list = [" + list + "]"); Set set = new HashSet(); List<String> listNew=new ArrayList<>(); set.addAll(list); listNew.addAll(set); System.out.println("listNew = [" + listNew + "]"); } //set去重(缩减为一行) public static void removeDuplicate4(List<String> list){ System.out.println("list = [" + list + "]"); List<String> listNew=new ArrayList<>(new HashSet(list)); System.out.println("listNew = [" + listNew + "]"); } //去重并按自然顺序排序 public static void removeDuplicate5(List<String> list){ System.out.println("list = [" + list + "]"); List<String> listNew=new ArrayList<>(new TreeSet<String>(list)); System.out.println("listNew = [" + listNew + "]"); }
或者Java8写法
List<String> list = list.stream().distinct().collect(Collectors.toList());
二、List中对象去重
重写Student对象的equals()方法和hashCode()方法
public class Student { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (!id.equals(student.id)) return false; return name.equals(student.name); } @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + name.hashCode(); return result; } }
Student stu1 = new Student(1L,"aaaa"); Student stu2 = new Student(2L,"dddd"); Student stu3 = new Student(1L,"aaaa"); Student stu4 = new Student(4L,"rrrr"); Student stu5 = new Student(1L,"ssss"); List<Student> students = Arrays.asList(stu1,stu2,stu3,stu4,stu5); // 方式一: 利用set特性 Set set = new HashSet(); List<Student> listNew = new ArrayList<>(); set.addAll(students); //students.clear(); listNew.addAll(set); System.out.println(listNew.toString()); System.out.println("listNew = [" + listNew + "]"); // 方式二:一行代码 必须重写equals和hashcode方法 List<Student> listNew2 = new ArrayList<>(new HashSet<>(students)); System.out.println("listNew2 = [" + listNew2 + "]"); // 方式三:java8写法 List<Student> studentsNew = new ArrayList<>(); students.stream().forEach( s -> { if (!studentsNew.contains(s)) { studentsNew.add(s); } } ); System.out.println("studentsNew = [" + studentsNew + "]");
三、根据对象的属性去重
// 传统写法 List<Student> listNew3 = new ArrayList<>(); Set<Student> studentSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId())); studentSet.addAll(students); listNew3.addAll(studentSet); System.out.println("listNew3 = [" + listNew3 + "]"); // Java8写法 List<Student> listNew4 = students.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Student::getId))), ArrayList::new)); System.out.println("listNew4 = [" + listNew4 + "]");
以上是关于List集合去重的主要内容,如果未能解决你的问题,请参考以下文章
--------------------------------------集合List去重总结------------------------