HoneyMoose
  • 首页
  • Java
  • Jersey
  • Jira
  • Confluence
  • U.S.
    • USRealEstate
    • USVisaTrack
    • H1B
  • 项目和网站
    • CWIKI.US
    • OSSEZ 社区
    • WIKI.OSSEZ.COM
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
BLOG.OSSEZ.COM
A responsive blog theme focused om user reading experience
  1. Home
  2. Computer Science
  3. This article

Java 8 使用 Stream 把 List 转换为 map

2023年01月13日 89Browse 0Like 0Comments

有这么一个小需求,有 2 个 List,但是我们希望返回 Map。

List 1 的数据到大于 List 2 中的数据。

返回 List1 的 map,如果 List 中的数据在 List 2 中存在的话,Map 的值是 True,如果不存在的话,是 False。

List1 和 List2 中的元素都是整数。

Stream

我们使用了 Java 提供的 Stream,当然你也可以用 For 循环。

下面的 map1 和 map 2 是等价的。

        List<Integer> reqIds = Arrays.asList(1, 2);
        List<Integer> reqs = Arrays.asList(1);
        Map<Integer, Boolean> map1 = reqIds.stream().collect(Collectors.toMap(Function.identity(), item -> reqs.contains(item)));
        Map<Integer, Boolean> map2 = reqIds.stream().collect(Collectors.toMap(Function.identity(), reqs::contains));

        log.debug("Map Size {}",map2);

 

2023-01-13_06-25-21

 

然后验证下结果。

 

https://www.ossez.com/t/java-8-stream-list-map/14296

Tags: None
Last updated:2023年01月13日

Kratos

stay absorbed stay excellent

Like
< Previous
Next >

Comments

Cancel reply

Newest Hotspots Random
Newest Hotspots Random
IBM Semeru Windows 下的安装 JDK 17 Okhttp 的 retrofit2 运行警告信息异常 应该升级 JDK 到 17 了吗 OpenAI-J 如何进行测试 微信 API 的发送客服消息返回对象不正确 PHP Windows 下 XAMPP 的 xdebug 配置
ChatGPT 嵌入微信公众号中的访问关于 USRealEstate 公众号的重要更新PHP 的 json_encode 异常Window 的 PHP XAMPP 安装 mongodb 的扩展PHP Windows 下 XAMPP 的 xdebug 配置微信 API 的发送客服消息返回对象不正确
IBM Semeru Windows 下的安装 JDK 17 IntelliJ IDEA 简体中文字体 Confluence 6 发送 Confluence 通知到其他 Confluence 服务器 OpenSearch 文档中文本地化 Windows 10 中的 Py 和 python 命令行 简述一下maven在 Java 项目开发管理中的作用
Categories
  • Algorithm(算法)
  • AMQP
  • Angular
  • CI
  • Compile And CI
  • Computer Science
  • Confluence
  • DataBase
  • Gradle
  • H1B
  • Hibernate
  • IDE
  • Java
  • Jersey
  • Jira
  • MariaDB
  • PrestaShop
  • Spring
  • Spring Batch
  • U.S.
  • USRealEstate
  • USVisaTrack
  • 我的小厨

COPYRIGHT © 2020 HoneyMoose. ALL RIGHTS RESERVED.

THEME CWIKIUS.CN MADE BY VTROIS