package com; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import java.util.*; import java.util.stream.Collectors; @Slf4j public class JunitTest{
//排序 @Test public void sort(){ List<Integer> lists = Arrays.asList(1,1,2,3); // 升序 lists.sort(Comparator.comparing(Integer::intValue)); lists.stream().sorted().collect(Collectors.toList()); // 降序 lists.sort(Comparator.comparing(Integer::intValue).reversed()); lists.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); //集合对象排序 List<User> list = new ArrayList<User>(); list.add(new User(5,"zhagnsan",18)); list.add(new User(4,"libai",19)); list.add(new User(1,"wangwu",22)); list.add(new User(2,"lihua",11)); //根据ID排序-1 list.sort(Comparator.comparing(User::getId)); //根据ID排序-2 Collections.sort(list,new Comparator<User>(){ @Override public int compare(User o1, User o2) { return o1.getId() - o2.getId(); } }); } //去重 @Test public void removal() { //1.使用LinkedHashSet删除arraylist中的重复数据 //删除重复数据 //保持添加到其中的数据的顺序 ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3)); System.out.println(arrayList); LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>(arrayList); ArrayList<Integer> integers = new ArrayList<>(linkedHashSet); System.out.println(integers); //2.使用java8新特性stream进行List去重 //java 8 stream api。使用steam的distinct()方法返回一个由不同数据组成的流,通过对象的equals()方法进行比较。 //收集所有区域数据List使用Collectors.toList() arrayList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3)); System.out.println(arrayList); List<Integer> integerList = arrayList.stream().distinct().collect(Collectors.toList()); System.out.println(integerList); //3.用HashSet不能添加重复数据的特性 // 由于HashSet不能保证添加顺序,所以只能作为判断条件保证顺序: arrayList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3)); HashSet<Integer> set = new HashSet<Integer>(arrayList.size()); List<Integer> result = new ArrayList<Integer>(arrayList.size()); for (Integer str : arrayList) { if (set.add(str)) { result.add(str); } } arrayList.clear(); arrayList.addAll(result); //4.用List的contains方法循环遍历,重新排序,只添加一次数据,避免重复 arrayList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3)); List<Integer> integerArrayList = new ArrayList<Integer>(arrayList.size()); for (Integer integer : integerArrayList) { if (!result.contains(integer)) { result.add(integer); } } arrayList.clear(); arrayList.addAll(result); //5.双重for循环去重 arrayList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3)); for (int i = 0; i < arrayList.size(); i++) { for (int j = 0; j < arrayList.size(); j++) { if (i != j && arrayList.get(i) == arrayList.get(j)) { arrayList.remove(arrayList.get(j)); } } } //6.根据年龄的性别去重 List<User> list = new ArrayList<User>(); list.add(new User(5,"zhagnsan",18)); list.add(new User(4,"libai",19)); list.add(new User(1,"wangwu",18)); list.add(new User(2,"lihua",11)); ArrayList<User> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(User::getAge))), ArrayList::new)); System.out.println(collect); }
@Data class User{ private int id; private String name; private int age; private User(int id,String name,int age){ this.id = id; this.name = name; this.age = age; } } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/18522.html