Java8的 stream对list数据去重,使用filter()过滤列表,list转map,joining使用,统计分组合并

使用 Java8的 stream对list数据去重,使用filter()过滤列表,list转map

1.去除List中重复的String

List unique = list.stream().distinct().collect(Collectors.toList());

2.去除重复对象

// 根据name去重
List<Person> unique = persons.stream().collect(
            Collectors.collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)
);

// 根据name,sex两个属性去重
List<Person> unique = persons.stream().collect(
           Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);

//基于java8 过滤器实现
private static List<UserCar> removeDuplicates3(List<UserCar> cars) {
        // 用于临时存放id
        List<String> filterCars = new ArrayList<>();

        return cars.stream().filter(
                e -> {
                    boolean found = !filterCars.contains(e.getCarNo());
                    filterCars.add(e.getCarNo());
                    return found;
                }
        ).collect(Collectors.toList());
    }


3.从一个Person对象的List集合,取出id和name组成一个map集合

Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));

4.从 List 中取出某个属性的组成 list 集合

//1.提取出list对象中的一个属性
List<String> stIdList1 = stuList.stream().map(Person::getId).collect(Collectors.toList());

//2.提取出list对象中的一个属性并去重
List<String> stIdList2 = stuList.stream().map(Person::getId).distinct().collect(Collectors.toList());

Student类

@Data
@ToString
@AllArgsConstructor
public class Student implements Serializable {
   private String id;
   private String name;
   private int age;
   private String address;
}
List<Student> studentArrayList = new ArrayList<>();
studentArrayList.add(new Student("000001", "张三", 12, "江苏省苏州市"));
studentArrayList.add(new Student("000002", "李四", 13, "江苏省南京市"));
studentArrayList.add(new Student("000003", "李红", 12, "安徽省合肥市"));

5.筛选filter
示例:筛选学生年龄小于13岁的学生数据

List<Student> studentList = studentArrayList.stream().filter(item -> item.getAge() < 13).collect(Collectors.toList());
System.out.println("studentList = " + studentList);

输出:

studentList = [Student(id=000001, name=张三, age=12, address=江苏省苏州市), Student(id=000003, name=李红, age=12, address=安徽省合肥市)]

6.最大值max、最小值min
示例:输出所有学生信息中年龄最大以及年龄最小的学生信息

Optional<Student> ageMax = studentArrayList.stream().max(Comparator.comparing(item -> item.getAge()));
// 使用isPresent方法判断是否有值,否则遇到null是直接get()操作引发异常
if (ageMax.isPresent()) {
 Student student = ageMax.get();
 System.out.println("student = " + student);
}
Optional<Student> ageMin = studentArrayList.stream().min(Comparator.comparing(item -> item.getAge()));
// 使用isPresent方法判断是否有值,否则遇到null是直接get()操作引发异常
if (ageMax.isPresent()) {
 Student student = ageMin.get();
 System.out.println("student = " + student);
}

输出:

student = Student(id=000002, name=李四, age=13, address=江苏省南京市) student = Student(id=000001, name=张三, age=12, address=江苏省苏州市)

7.统计count
示例:统计所有学生中年龄小于13岁的人数

long count = studentArrayList.stream().filter(item -> item.getAge() < 13).count();
System.out.println("count = " + count);

输出:

count = 2

8.接合joining
joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
示例:将所有学生的姓名输出,并用,作分隔符

String collect = studentArrayList.stream().map(item -> item.getName()).collect(Collectors.joining(","));
System.out.println("collect = " + collect);

输出:

collect = 张三,李四,李红

9.分组groupingBy
示例:将所有学生信息按性别分组

Map<Integer, List<Student>> collect = studentArrayList.stream().collect(Collectors.groupingBy(Student::getAge));
System.out.println("collect = " + collect);

输出:

collect = {12=[Student(id=000001, name=张三, age=12, address=江苏省苏州市), Student(id=000003, name=李红, age=12, address=安徽省合肥市)], 13=[Student(id=000002, name=李四, age=13, address=江苏省南京市)]}

10.流的合并concat、去重distinct、限制limit、跳过skip
示例:将两个stream进行合并,并作去重处理

Stream<String> stream1 = CollUtil.toList("aa", "bb", "cc", "dd").stream();
Stream<String> stream2 = CollUtil.toList("bb", "cc", "ee", "ff").stream();
List<String> stringList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
System.out.println("stringList = " + stringList);

输出:

stringList = [aa, bb, cc, dd, ee, ff]

示例:从1开始,输出前10个奇数值

List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
System.out.println("collect = " + collect);

输出:

collect = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

示例:从1开始,跳过前2个元素,输出前6个奇数元素

List<Integer> collect = Stream.iterate(1, x -> x + 2).skip(2).limit(6).collect(Collectors.toList());
System.out.println("collect = " + collect);

输出:

collect = [5, 7, 9, 11, 13, 15]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值