开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
  1. Home
  2. Computer Science
  3. This article

Java 如何将 List 转换为 MAP

2022年10月12日 588Browse 0Like 0Comments

有时候我们需要将给定的 List 转换为 Map。

如果你使用的是 Java 8 以后版本的话,Stream 是你的好朋友。

Java 8 map example 2

Java 8

 public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
    Map<Integer, Animal> map = list.stream()
      .collect(Collectors.toMap(Animal::getId, Function.identity()));
    return map;
}

上面的代码可以非常容易的完成转换,我们有一个 Animal 对象的 List。

上面的代码将会把 Id 作为 Key,然后生成的 Map 是以 id 为 Key,Animal 为Value 的 Map。

Guava

如果使用 Guava 就更加简单了。

public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
    Map<Integer, Animal> map = Maps
      .uniqueIndex(list, Animal::getId);
    return map;
}

使用 Maps 的工具类就可以了,这个工具类可以直接用。

更进一步

如果对需要生成的 Map 进行处理。

Key 是对象中的一个值,Value 是 List 对象中的另外一个值。

例如可以使用下面的代码:

listingStatusList.stream().collect(Collectors.toMap(CListingStatus::getListingStatusKey, CListingStatus::getListingStatus));

其中 CListingStatus 对象是这样定义的。

@Entity
@Getter
@Setter
public class CListingStatus extends AbstractPersistable<Long> {

    @ManyToOne
    @JoinColumn(name = "rets_id", nullable = false)
    private ConfRets confRets;

    private String listingStatusKey;

    private ListingStatus listingStatus ;

}

对 Map 中的对象设值

针对 Stream 中的对象,我们可能还需要重新设置为其他的对象。

这个时候我们就可以使用 lambda 函数了。

同样的代码:

        HashMap<String, Agent> agentHashMap = (HashMap) mlsAgentList.stream().collect(Collectors.toMap(MlsAgent::getMlsAgentId, mlsAgent -> {
            Agent agent = new Agent();
            agent.setAgentId(mlsAgent.getMlsAgentId());
            agent.setAgentNameFirst(mlsAgent.getNameFirst());
            agent.setAgentNameLast(mlsAgent.getNameLast());
            agent.setAgentEmail(mlsAgent.getEmail());
            return agent;
        }));

我们返回的 Map 使用了一个新的对象为 Value。

上面针对 Stream 转换为 Map 的方法进行了一些小总结,这些方法可能实际编程的时候使用的频率比较高。

同时能够避免大量使用 For 循环的情况。

Stream 还是需要好好了解下的。

 

https://www.ossez.com/t/java-list-map/14144

Tags: None
Last updated:2022年10月12日

HoneyMoose

有温度的人文和独立的思考

Like
< Previous
Next >

Comments

Cancel reply

Archives
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,362)
    • Confluence (663)
    • Gradle (12)
  • U.S. (482)
  • 文化旅游 (145)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1