Java 8 将List转换为Map_java stream流list转map

CSDN博客 · · 683 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

几个Java 8示例来向您展示如何将一个List对象转换为一个Map,以及如何处理重复的键

Hosting.java
  1. package com.mkyong.java8
  2. public class Hosting {
  3. private int Id;
  4. private String name;
  5. private long websites;
  6. public Hosting(int id, String name, long websites) {
  7. Id = id;
  8. this.name = name;
  9. this.websites = websites;
  10. }
  11. //getters, setters and toString()
  12. }

List到Map - Collectors.toMap()

创建Hosting对象的List,Collectors.toMap并将其转换为Map。

TestListMap.java
  1. package com.mkyong.java8
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6. public class TestListMap {
  7. public static void main(String[] args) {
  8. List<Hosting> list = new ArrayList<>();
  9. list.add(new Hosting(1, "liquidweb.com", 80000));
  10. list.add(new Hosting(2, "linode.com", 90000));
  11. list.add(new Hosting(3, "digitalocean.com", 120000));
  12. list.add(new Hosting(4, "aws.amazon.com", 200000));
  13. list.add(new Hosting(5, "mkyong.com", 1));
  14. // key = id, value - websites
  15. Map<Integer, String> result1 = list.stream().collect(
  16. Collectors.toMap(Hosting::getId, Hosting::getName));
  17. System.out.println("Result 1 : " + result1);
  18. // key = name, value - websites
  19. Map<String, Long> result2 = list.stream().collect(
  20. Collectors.toMap(Hosting::getName, Hosting::getWebsites));
  21. System.out.println("Result 2 : " + result2);
  22. // Same with result1, just different syntax
  23. // key = id, value = name
  24. Map<Integer, String> result3 = list.stream().collect(
  25. Collectors.toMap(x -> x.getId(), x -> x.getName()));
  26. System.out.println("Result 3 : " + result3);
  27. }
  28. }

Output

  1. Result 1 : {1=liquidweb.com, 2=linode.com, 3=digitalocean.com, 4=aws.amazon.com, 5=mkyong.com}
  2. Result 2 : {liquidweb.com=80000, mkyong.com=1, digitalocean.com=120000, aws.amazon.com=200000, linode.com=90000}
  3. Result 3 : {1=liquidweb.com, 2=linode.com, 3=digitalocean.com, 4=aws.amazon.com, 5=mkyong.com}

2.列出Map - 重复键

2.1运行下面的代码,重复的关键错误将被抛出!

TestDuplicatedKey.java
  1. package com.mkyong.java8;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6. public class TestDuplicatedKey {
  7. public static void main(String[] args) {
  8. List<Hosting> list = new ArrayList<>();
  9. list.add(new Hosting(1, "liquidweb.com", 80000));
  10. list.add(new Hosting(2, "linode.com", 90000));
  11. list.add(new Hosting(3, "digitalocean.com", 120000));
  12. list.add(new Hosting(4, "aws.amazon.com", 200000));
  13. list.add(new Hosting(5, "mkyong.com", 1));
  14. list.add(new Hosting(6, "linode.com", 100000)); // new line
  15. // key = name, value - websites , but the key 'linode' is duplicated!?
  16. Map<String, Long> result1 = list.stream().collect(
  17. Collectors.toMap(Hosting::getName, Hosting::getWebsites));
  18. System.out.println("Result 1 : " + result1);
  19. }
  20. }

输出 - 下面的错误信息有点误导,应该显示“linode”而不是键的值。

  1. Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90000
  2. at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
  3. at java.util.HashMap.merge(HashMap.java:1245)
  4. //...

2.2要解决上述重复的关键问题,请传入第三个mergeFunction参数,如下所示:

  1. Map<String, Long> result1 = list.stream().collect(
  2. Collectors.toMap(Hosting::getName, Hosting::getWebsites,
  3. (oldValue, newValue) -> oldValue
  4. )
  5. );

Output

Result 1 : {..., aws.amazon.com=200000, linode.com=90000}

3.3尝试newValue

  1. Map<String, Long> result1 = list.stream().collect(
  2. Collectors.toMap(Hosting::getName, Hosting::getWebsites,
  3. (oldValue, newValue) -> newvalue
  4. )
  5. );

Output

Result 1 : {..., aws.amazon.com=200000, linode.com=100000}

3.列出Map- 排序Collect

TestSortCollect.java
  1. package com.mkyong.java8;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. public class TestSortCollect {
  5. public static void main(String[] args) {
  6. List<Hosting> list = new ArrayList<>();
  7. list.add(new Hosting(1, "liquidweb.com", 80000));
  8. list.add(new Hosting(2, "linode.com", 90000));
  9. list.add(new Hosting(3, "digitalocean.com", 120000));
  10. list.add(new Hosting(4, "aws.amazon.com", 200000));
  11. list.add(new Hosting(5, "mkyong.com", 1));
  12. list.add(new Hosting(6, "linode.com", 100000));
  13. //example 1
  14. Map result1 = list.stream()
  15. .sorted(Comparator.comparingLong(Hosting::getWebsites).reversed())
  16. .collect(
  17. Collectors.toMap(
  18. Hosting::getName, Hosting::getWebsites, // key = name, value = websites
  19. (oldValue, newValue) -> oldValue, // if same key, take the old key
  20. LinkedHashMap::new // returns a LinkedHashMap, keep order
  21. ));
  22. System.out.println("Result 1 : " + result1);
  23. }
  24. }

Output

Result 1 : {aws.amazon.com=200000, digitalocean.com=120000, linode.com=100000, liquidweb.com=80000, mkyong.com=1}





文章知识点与官方知识档案匹配,可进一步学习相关知识

本文来自:CSDN博客

感谢作者:CSDN博客

查看原文:Java 8 将List转换为Map_java stream流list转map

683 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传