java8List 元素对象单字段去重 多字段去重 交集 差集 多字段排序

1.排序

List list = new ArrayList<>();
list.add(new User(1, “张三”));
list.add(new User(4, “赵六”));

    list.forEach(f -> f.setUserName("zhao"));
    System.out.println(list);
     list.forEach(f  - > f.setName(f.getName( )+"s"))

    //条件删除
     list.removeIf(user -> user.getUserId() == 3);
    System.out.println("--------------排序前");
    list.forEach(user -> {
        System.out.println(user);
    });
     list.sort((user1,user2)->user1.getUserId() - user2.getUserId());
    System.out.println("_-------------------正序排序后");
    list.forEach(user -> {
        System.out.println(user);
    });

    System.out.println("--------------------倒序排序后:");
     list.sort((((o1, o2) -> o2.getUserId() - o1.getUserId())));
    list.forEach(user -> {
        System.out.println(user);
    });
	 //多字段排序
    //ArrayList<Student> oldList1 = oldList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(table -> table.getAge() + ";" + table.getNo()))), ArrayList::new));

2.list 转成 map ;key:name value:stockCode

List list = new ArrayList();
list.add(new User1(“zhao”, 1, “000002”, “万科A”));
list.add(new User1(“zhao”, 1, “000002”, “万科A”));
list.add(new User1(“zhao”, 1, “000002”, “万科A12”));
//key-value (String-String) key对应字符
Map<String, String> userMap = list.stream().collect(Collectors.toMap(User1::getName, User1::getStockCode, (k1, k2) -> k2));
//key-value (String-entity) key对应对象
Map<String, User1> list2 = list.stream().collect(Collectors.toMap(User1::getName, Function.identity(), (key1, key2) -> key2));
//key-value (String-entity) 多字段拼接key对应对象
Map<String, User1> list3 = list.stream().collect(Collectors.toMap(item->item.getName()+item.getAge(),Function.identity(), (key1, key2) -> key2));
//key-value (String-list) key对应list
Map<String, List> list4= list.stream().collect(Collectors.groupingBy(t->t.getName()));

3. list 去重

List aa = list1.stream().distinct().collect(Collectors.toList());

4.list根据对象某一字段值去重

System.out.println(“--------list根据对象某一字段值去重--------”);
List userList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User1::getName))), ArrayList::new));

5.list多属性去重 辩解

List list1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(u -> u.getName() + “;” + u.getAge()))), ArrayList::new));

6.list 取交集

List intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());

7.获取元素对象属性值集合

8.List 根据Bean的一个属性求两个list的交集 差集

9.list 和 List 取差集

Student s1 = new Student(11, “black”, 456);
Student s2 = new Student(12, “red”, 123);//old
Student s6 = new Student(13, “red”, 123);//old

    Student s3 = new Student(11, "black", 123);
    Student s4 = new Student(12, "red1", 123);
    Student s5 = new Student(15, "white", 123);//new

    List<Student> oldList = new ArrayList<Student>() {{
        add(s1);
        add(s2);
        add(s6);
    }};
    List<Integer> oldAgeList = oldList.stream().map(x -> x.getAge()).collect(Collectors.toList());

    List<Student> newList = new ArrayList<Student>() {{
        add(s3);
        add(s4);
        add(s5);
    }};
    //获取元素对象属性值集合
    List<Integer> newAgeList = oldList.stream().map(x -> x.getAge()).collect(Collectors.toList());

    //List<Bean> 根据Bean的一个属性求两个list的交集 差集
    //交集
    List<Student> updList = newList.stream()
            .filter(item -> oldList.stream()
                    .map(e -> e.getAge())
                    .collect(Collectors.toList())
                    .contains(item.getAge()))
            .collect(Collectors.toList());
    System.out.println(updList);

    // 差集 (new - old)
    List<Student> addList = newList.stream()
            .filter(item -> !oldList.stream()
                    .map(e -> e.getAge())
                    .collect(Collectors.toList())
                    .contains(item.getAge()))
            .collect(Collectors.toList());
    System.out.println(addList);

    // 差集 (old - new)
    List<Student> delList = oldList.stream()
            .filter(item -> !newList.stream()
                    .map(e -> e.getAge())
                    .collect(Collectors.toList())
                    .contains(item.getAge()))
            .collect(Collectors.toList());
    System.out.println(delList);
    //list<String> 和 List<Bean> 取差集
    List<String> list3 = new ArrayList<String>() {{
        add("aa");
        add("black");
        add("black");
    }};
    List<String> listStr = list3.stream()
            .filter(item -> !newList.stream()
                    .map(e -> e.getHaircolor())
                    .collect(Collectors.toList())
                    .contains(item))
            .collect(Collectors.toList());
    System.out.println(listStr);

10.list 获取元素对象单属性值重复的字段数据 返回String

List uniqueList = list.stream().collect(Collectors.groupingBy(User1::getStockCode, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());

11.list 获取元素对象多属性值重复的字段数据 返回String

List uniqueList2 = list.stream().collect(Collectors.groupingBy(user1 -> user1.getStockCode() + “#” + user1.getAge() + “#” + user1.getStockName(), Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey).collect(Collectors.toList());

12.list 条件过滤

List<User1> ss = list.stream().filter(user -> (user.getName().equals("zhao") && user.getAge() == Integer.valueOf(1))).collect(Collectors.toList());			

13.多字段排序

ArrayList oldList1 = oldList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(table -> table.getAge() + “;” + table.getNo()))), ArrayList::new));

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值