0
点赞
收藏
分享

微信扫一扫

使用Java8的Stream流优雅的操作Map

一、前言

在Java 8中引入的Stream API为集合操作提供了一种声明式的编程风格。本文将通过几个示例来展示如何使用Stream API来操作Map对象,包括过滤、映射、排序等常见操作。

二、项目实践

1.创建测试实体类

package com.example.springbootdemo.test;

public class Student {

    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.创建测试数据

package com.example.springbootdemo.test;

import java.util.HashMap;
import java.util.Map;

public class StudentUtil {

    public static Map<String, Student> getStudentData() {
        Map<String, Student> studentMap = new HashMap<>();
        studentMap.put("aa", new Student("aa", 20));
        studentMap.put("bb", new Student("bb", 21));
        studentMap.put("cc", new Student("cc", 22));
        studentMap.put("dd", new Student("dd", 23));
        return studentMap;
    }
}

3.过滤Map

如果我们想找出所有年龄大于22岁的人员,可以使用stream()方法将Map转换成流,然后使用filter()方法过滤出满足条件的对象。

package com.example.springbootdemo.test;

import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        Map<String, Student> studentMap = StudentUtil.getStudentData();
        //过滤大于22岁人员
        Map<String, Student> map = studentMap.entrySet().stream().filter(s -> s.getValue().getAge() > 22).collect(Collectors.toMap(s -> s.getKey(), s -> s.getValue()));
        System.out.println(map);
    }
}

运行结果:

使用Java8的Stream流优雅的操作Map_Map

4.映射 Map

如果我们要从Map中提取所有人的名字并存储在一个列表中,可以使用map()方法来实现。

package com.example.springbootdemo.test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        Map<String, Student> studentMap = StudentUtil.getStudentData();
        List<String> nameList = studentMap.values().stream().map(Student::getName).collect(Collectors.toList());
        System.out.println(nameList);
    }
}

运行结果:

使用Java8的Stream流优雅的操作Map_Stream_02

5.排序 Map

对Map进行排序通常需要先将其转换为流,然后使用sorted()方法排序后再收集回Map或列表。例如,按照年龄排序:

package com.example.springbootdemo.test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        Map<String, Student> studentMap = StudentUtil.getStudentData();
        //大到小排序
        List<Student> studentList = studentMap.values().stream().sorted((s1, s2) -> s2.getAge() - s1.getAge()).collect(Collectors.toList());
        System.out.println(studentList);
        //小到大排序
        List<Student> studentList1 = studentMap.values().stream().sorted((s1, s2) -> s1.getAge() - s2.getAge()).collect(Collectors.toList());
        System.out.println(studentList1);

    }
}

运行结果:

使用Java8的Stream流优雅的操作Map_Java_03

6.高级操作-多重过滤与排序

我们可以结合多个过滤器和排序器来执行更复杂的操作。比如,筛选年龄大于21岁的人,并按年龄降序排列:

package com.example.springbootdemo.test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        Map<String, Student> studentMap = StudentUtil.getStudentData();
        //先过滤 再排序
        List<Student> studentList = studentMap.values().stream().filter(s -> s.getAge() > 21).sorted((s1, s2) -> s2.getAge() - s1.getAge()).collect(Collectors.toList());
        System.out.println(studentList);
    }
}

运行结果:

使用Java8的Stream流优雅的操作Map_Java_04

三、总结

通过上面的例子,我们可以看到Java Stream API使得操作Map变得既简洁又强大。无论是基本的过滤和映射操作,还是复杂的排序和组合过滤,都可以轻松完成。

举报

相关推荐

0 条评论