java lambda 有效实例(结合List,Map数据结构)

1. 编写目的

总结一下这么长时间使用 java 的 lambda 表达式的方法,也方便以后使用。

2. map 操作

map 应该理解为建立一个映射关系,将已有的数据映射到另外一种格式或者放入某个函数中得到新的数值。

2.1 map 基础操作(结合list)

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5, 42, 99, 323);

// 1. 将每个数值进行平方,并转换回 list
List<Double> result1 = numbers.stream().map(e -> Math.pow(e, 2)).collect(Collectors.toList());
result1.forEach(System.out::println);

// 2. 转换为 double 类型
List<Double> result2 = numbers.stream().map(e -> e * 1.0).collect(Collectors.toList());
result2.forEach(System.out::println);

// 等同于 map(e->Double.valueOf(e))
List<Double> result3 = numbers.stream().map(Double::valueOf).collect(Collectors.toList());
result3.forEach(System.out::println);

// 转换为字符串
List<String> result4 = numbers.stream().map(e -> e + "").collect(Collectors.toList());
result4.forEach(System.out::println);

// 3. 结合 mapTo 来求和
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
System.out.println(sum);

// 4. 获取对应的平方数
List<Integer> result5 = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
result5.forEach(System.out::println);

2.2 map 用于“解包”

比如说我们拿到一个 List<Pair<String, Double>> 类型的学生成绩类型的数据,需要从中提取出所有 Double 类型的数据,组成新的 List。

// 制造假数据
LinkedList<Pair<String, Double>> students = new LinkedList<>();
students.add(new Pair<>("xiaoming", 98.5));
students.add(new Pair<>("xiaofang", 98.8));
students.add(new Pair<>("xiaoli", 68.8));
students.add(new Pair<>("xiaogabg", 48.8));
students.add(new Pair<>("xiaohe", 73.4));

// 1. 拿到所有人的姓名
List<String> names = students.stream().map(e -> e.getKey()).collect(Collectors.toList());
names.forEach(System.out::println);

// 打印
// xiaoming
// xiaofang
// xiaoli
// xiaogabg
// xiaohe
// 2. 拿到所有人的分数
List<Double> scores = students.stream().map(Pair::getValue).collect(Collectors.toList());
scores.forEach(System.out::println);
// 98.5
// 98.8
// 68.8
// 48.8
// 73.4

这个例子比较简单,当遇到复杂的数据的时候可以多层 map,一层一层的解开。

3. filter 操作

过滤功能,需要注意的是 返回的是符合条件的

// 制造假数据
LinkedList<Pair<String, Double>> students = new LinkedList<>();
students.add(new Pair<>("xiaoming", 98.5));
students.add(new Pair<>("xiaofang", 98.8));
students.add(new Pair<>("xiaoli", 68.8));
students.add(new Pair<>("xiaogabg", 48.8));
students.add(new Pair<>("xiaohe", 73.4));

// 1. 成绩超过 90 分的
List<Pair<String, Double>> some = students.stream().filter(e -> e.getValue() > 90).collect(Collectors.toList());
some.forEach(System.out::println);
// 打印内容
// xiaoming=98.5
// xiaofang=98.8

// 2. 成绩超过 90 的名字
List<String> some2 = students.stream().filter(e -> e.getValue() > 90).map(e -> e.getKey()).collect(Collectors.toList());
some2.forEach(System.out::println);
// 打印
// xiaoming
// xiaofang

4. Collectors 操作

前面一直在用 Collectors.toList 方法,还有其他用法主要如下。

4.1 toSet 去重

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5, 42, 99, 323);

// toSet 去重
Set<Integer> result1 = numbers.stream().collect(Collectors.toSet());
result1.forEach(System.out::println);
// 2
// 3
// 99
// 323
// 5
// 7
// 42

4.2 toList

前面的例子一直在用这个方法,这里不介绍了。

4.3 groupBy 分组操作

// 制造假数据
LinkedList<Pair<String, Double>> students = new LinkedList<>();
students.add(new Pair<>("xiaoming", 98.5));
students.add(new Pair<>("xiaoming", 98.5));
students.add(new Pair<>("xiaoming", 98.5));
students.add(new Pair<>("xiaofang", 98.8));
students.add(new Pair<>("xiaoli", 68.8));
students.add(new Pair<>("xiaogabg", 48.8));
students.add(new Pair<>("xiaohe", 73.4));
students.add(new Pair<>("xiaohe", 73.4));
students.add(new Pair<>("xiaohe", 73.4));

Map<String, List<Pair<String, Double>>> group = students.stream().collect(Collectors.groupingBy(Pair::getKey));
group.entrySet().forEach(e->{
    System.out.print("分组:"+e.getKey()+":\t");
    System.out.print(e.getValue()+"\n");
});
//        分组:xiaogabg:	[xiaogabg=48.8]
//        分组:xiaoming:	[xiaoming=98.5, xiaoming=98.5, xiaoming=98.5]
//        分组:xiaohe:	[xiaohe=73.4, xiaohe=73.4, xiaohe=73.4]
//        分组:xiaoli:	[xiaoli=68.8]
//        分组:xiaofang:	[xiaofang=98.8]

另一个示例,用来统计一个数组中各个值出现的次数,与前一个例子相比,这个更加直接,解包出所有结果,并进行了排序得到链表。

Integer[] ary = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3};

List<Pair<Integer, Integer>> intervalCountList = Arrays.stream(ary).collect(Collectors.groupingBy(e -> e))
        .entrySet().stream()
        .map(pair -> Pair.with(pair.getKey(), pair.getValue().size()))
        .sorted()
        .collect(Collectors.toList());
intervalCountList.forEach(System.out::println);

输出结果为:

[1, 3]
[2, 3]
[3, 3]
[4, 2]
[5, 2]

5. flatMap 操作

比如说一个二维数组,现在需要打平,得到一个一维链表,或进行更多操作。

这个例子使用 value 为链表的 map,得到一个一维链表。

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FlatMapDemo {
    public static void main(String[] args) {
        HashMap<Integer, List<Student>> studentsMap = new HashMap<>();
        int row = 5;
        int column = 8;
        for (int i = 0; i < row; i++) {
            LinkedList<Student> list = new LinkedList<>();
            for (int j = 0; j < column; j++) {
                list.add(new Student(i * column + j, (i * column + j) + ""));
            }
            studentsMap.put(i, list);
        }

        // 现在把 map 解析出来组成一个链表
        List<Student> allStudents = studentsMap.entrySet().stream()
                .flatMap(map -> map.getValue().stream()
                        .map(e -> new Student(e.getId(), e.getName()))).collect(Collectors.toList());
        allStudents.forEach(System.out::println);

        // 此时并没有达到解包的目的,依然是二维链表
        List<Stream<Student>> streamList = studentsMap.entrySet().stream().map(e -> e.getValue().stream()
                .map(v -> new Student(v.getId(), v.getName())))
                .collect(Collectors.toList());

        // 此时得到的是二维链表
        List<List<Student>> lists = studentsMap.values().stream().map(students -> students.stream()
                .map(v -> new Student(v.getId(), v.getName())).collect(Collectors.toList())).collect(Collectors.toList());

        // 等价于上面的内容
        List<List<Student>> lists2 = studentsMap.entrySet().stream().map(e -> e.getValue().stream()
                .map(v -> new Student(v.getId(), v.getName())).collect(Collectors.toList())).collect(Collectors.toList());

    }

    @Data
    @AllArgsConstructor
    public static class Student {
        private Integer id;
        private String name;
    }

}

6. 总结

只是对以前的比较进行补充,希望可以帮助到需要的小伙伴。致谢~

Smileyan
2022.6.21 19:57

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smile-yan

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值