Java 8 将List转换为Map
来源:互联网 发布:淘宝电子烟店铺排名 编辑:程序博客网 时间:2024/05/24 02:47
几个Java 8示例来向您展示如何将一个List
对象转换为一个Map
,以及如何处理重复的键
Hosting.java
package com.mkyong.java8public class Hosting { private int Id; private String name; private long websites; public Hosting(int id, String name, long websites) { Id = id; this.name = name; this.websites = websites; } //getters, setters and toString()}
List到Map - Collectors.toMap()
创建Hosting
对象的List,Collectors.toMap
并将其转换为Map。
TestListMap.java
package com.mkyong.java8import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class TestListMap { public static void main(String[] args) { List<Hosting> list = new ArrayList<>(); list.add(new Hosting(1, "liquidweb.com", 80000)); list.add(new Hosting(2, "linode.com", 90000)); list.add(new Hosting(3, "digitalocean.com", 120000)); list.add(new Hosting(4, "aws.amazon.com", 200000)); list.add(new Hosting(5, "mkyong.com", 1)); // key = id, value - websites Map<Integer, String> result1 = list.stream().collect( Collectors.toMap(Hosting::getId, Hosting::getName)); System.out.println("Result 1 : " + result1); // key = name, value - websites Map<String, Long> result2 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites)); System.out.println("Result 2 : " + result2); // Same with result1, just different syntax // key = id, value = name Map<Integer, String> result3 = list.stream().collect( Collectors.toMap(x -> x.getId(), x -> x.getName())); System.out.println("Result 3 : " + result3); }}
Output
Result 1 : {1=liquidweb.com, 2=linode.com, 3=digitalocean.com, 4=aws.amazon.com, 5=mkyong.com}Result 2 : {liquidweb.com=80000, mkyong.com=1, digitalocean.com=120000, aws.amazon.com=200000, linode.com=90000}Result 3 : {1=liquidweb.com, 2=linode.com, 3=digitalocean.com, 4=aws.amazon.com, 5=mkyong.com}
2.列出Map - 重复键
2.1运行下面的代码,重复的关键错误将被抛出!
TestDuplicatedKey.java
package com.mkyong.java8;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class TestDuplicatedKey { public static void main(String[] args) { List<Hosting> list = new ArrayList<>(); list.add(new Hosting(1, "liquidweb.com", 80000)); list.add(new Hosting(2, "linode.com", 90000)); list.add(new Hosting(3, "digitalocean.com", 120000)); list.add(new Hosting(4, "aws.amazon.com", 200000)); list.add(new Hosting(5, "mkyong.com", 1)); list.add(new Hosting(6, "linode.com", 100000)); // new line // key = name, value - websites , but the key 'linode' is duplicated!? Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites)); System.out.println("Result 1 : " + result1); }}
输出 - 下面的错误信息有点误导,应该显示“linode”而不是键的值。
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90000at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)at java.util.HashMap.merge(HashMap.java:1245)//...
2.2要解决上述重复的关键问题,请传入第三个mergeFunction参数,如下所示:
Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites, (oldValue, newValue) -> oldValue ) );
Output
Result 1 : {..., aws.amazon.com=200000, linode.com=90000}
3.3尝试newValue
Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites, (oldValue, newValue) -> newvalue ) );
Output
Result 1 : {..., aws.amazon.com=200000, linode.com=100000}
3.列出Map- 排序Collect
TestSortCollect.java
package com.mkyong.java8;import java.util.*;import java.util.stream.Collectors;public class TestSortCollect { public static void main(String[] args) { List<Hosting> list = new ArrayList<>(); list.add(new Hosting(1, "liquidweb.com", 80000)); list.add(new Hosting(2, "linode.com", 90000)); list.add(new Hosting(3, "digitalocean.com", 120000)); list.add(new Hosting(4, "aws.amazon.com", 200000)); list.add(new Hosting(5, "mkyong.com", 1)); list.add(new Hosting(6, "linode.com", 100000)); //example 1 Map result1 = list.stream() .sorted(Comparator.comparingLong(Hosting::getWebsites).reversed()) .collect( Collectors.toMap( Hosting::getName, Hosting::getWebsites, // key = name, value = websites (oldValue, newValue) -> oldValue, // if same key, take the old key LinkedHashMap::new // returns a LinkedHashMap, keep order )); System.out.println("Result 1 : " + result1); }}
Output
Result 1 : {aws.amazon.com=200000, digitalocean.com=120000, linode.com=100000, liquidweb.com=80000, mkyong.com=1}
阅读全文
0 0
- Java 8 将List转换为Map
- Java 8 将Map转换为List
- Java 8 – Convert List to Map(将 List 转换为 Map)
- Java实现将Map转换为List的小代码
- Java实现将Map转换为List的小代码
- Java 8 将Stream转换为List
- 将List<Map>/Map中的key转换为驼峰规则
- java方式的将java对象以及list或者map转化为json数据,json转换为object或者map
- java 将jso 格式的字符串转换为List<Map<String,String>>
- java 后台使用gson 将json字符串转换为List<Map<String,object>> 数组
- java 将list转换为map 的泛型通用方法
- list转换为map
- java 将一个listBean转换为map
- 将map转换为java对象
- 将对象、Map或者List转换为Json串
- Java 8 – 将 Stream 转换为 List
- 【深入JAVA】将Json字符串转换成Map/List
- java中javabean、list、map格式转换为json格式
- Lua 元表(metatable)与元方法(metamethod)
- initializationError [Runner:JUnit 4]:报错问题
- 做开发对产品的理解 -- 1
- bash变量
- 通俗易懂详解iptables
- Java 8 将List转换为Map
- 鼠标移入显示二维码(原生、JQURY两种方式)
- Linux下搭建集群环境(3)-----------linux下安装tomcat
- Android —— SQLite数据库简易使用
- LintCode 关于排序问题的总结
- java 构造函数
- 网络 协议
- linux基础入门之passwd命令
- iOS开发证书说明