拾光记录
218个文档 导航 作者 Gitee
随笔记录4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端知识4
基础知识17
Vue框架19
UniApp14
微信小程序1
Java编程6
Java基础15
SpringBoot31
SpringMVC18
MyBatis9
SpringCloud15
中间件2
数据库4
MySQL13
Redis8
MongoDB10
其他数据库1
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv管理工具
Linux12
Linux常用命令Jar启动脚本VirtualBox安装CentOSVirtualBox安装Ubuntu树莓派安装及使用frp内网穿透ArchLinux:基础系统安装ArchLInux:图形化界面安装ArchLinux:常用软件ArchLinux:深度优化ArchLinux:NiriArchLinux:模块记录
软件工具12
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaRustFSPicGoVSCodeDocker
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

Lambda表达式

函数式编程,将方法的实现作为参数进行传递

Lambda的作用

函数式接口

Lambda表达式只能有一行代码的情况下可以简化,多行需要用代码块包裹

Lambda过滤

// 去除不存在用户
list = list.stream().filter(c -> c.getUser() != null).collect(Collectors.toList());

排序

// 流排序(升序)
articles.stream().sorted(Comparator.comparing(Article::getTitle)).collect(Collectors.toList())
// 流排序(降序)
articles.stream().sorted(Comparator.comparing(Article::getTitle).reversed()).collect(Collectors.toList())
// list排序
list.sort(Comparator.comparingInt(CommonType::getSortHot).reversed());

// 多个字段
articles.stream()
       .sorted(Comparator.comparing(Article::getTitle)
               .thenComparing(Article::getAuthor)
               .thenComparing(Article::getDate))
       .collect(Collectors.toList());

去重

// 去重
ArrayList<ClassAmountVo> collect = classExitsList.stream().collect(
	Collectors.collectingAndThen(
		Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ClassAmountVo::getClassId))),
		ArrayList::new
	)
);

// 字符去重
List<String> list=list.stream().distinct().collect(Collectors.toList());
// 关键字去重
String[] array = Arrays.stream(split).distinct().toArray(String[]::new);

获取某字段集合

List<String> collect = categoryList.stream().map(Category::getId).collect(Collectors.toList());

累加、最大、最小

Long countPayOrderIncome = payOrderList.stream().mapToLong(PayOrder::getTotal).sum();
Long countPayOrderIncome = payOrderList.stream().mapToLong(PayOrder::getTotal).max();
Long countPayOrderIncome = payOrderList.stream().mapToLong(PayOrder::getTotal).min();

计数

@Test
    public void countTest() {
        long count = list.stream().count();
        System.out.println(count);
    }
}

跳过、截取

/**
 * 有状态操作
 */
@Test
public void limitTest() {
    list.stream()
        .sorted(Comparator.comparing(Sku::getTotalPrice))
        .skip(2 * 3)
        .limit(3)
        .forEach(item -> System.out.println(JSON.toJSONString(item, true)));
}

查找第一个和任意一个

@Test
public void findFirstTest() {
    Optional<Sku> optional = list.stream()
        .peek(sku -> System.out.println(sku.getSkuName()))
        .findFirst();
    System.out.println(JSON.toJSONString(optional.get(), true));
}

@Test
public void findAnyTest() {
    Optional<Sku> optional = list.stream()
        .peek(sku -> System.out.println(sku.getSkuName()))
        .findAny();
    System.out.println(JSON.toJSONString(optional.get(), true));
}
Lambda表达式Lambda的作用函数式接口Lambda过滤排序去重获取某字段集合累加、最大、最小计数跳过、截取查找第一个和任意一个