package com.zzg.java8
public class webSite{
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()
创建webSite
对象的List,Collectors.toMap
并将其转换为Map。
package com.zzg.java8
import 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<webSite> list = new ArrayList<>();
list.add(new webSite(1, "liquidweb.com", 80000));
list.add(new webSite(2, "linode.com", 90000));
list.add(new webSite(3, "digitalocean.com", 120000));
list.add(new webSite(4, "aws.amazon.com", 200000));
list.add(new webSite(5, "mkyong.com", 1));
// key = id, value - websites
Map<Integer, String> result1 = list.stream().collect(
Collectors.toMap(webSite::getId, webSite::getName));
System.out.println("Result 1 : " + result1);
// key = name, value - websites
Map<String, Long> result2 = list.stream().collect(
Collectors.toMap(webSite::getName, webSite::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);
}
}
列出Map - 重复键
package com.zzg.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<webSite> list = new ArrayList<>();
list.add(new webSite(1, "liquidweb.com", 80000));
list.add(new webSite(2, "linode.com", 90000));
list.add(new webSite(3, "digitalocean.com", 120000));
list.add(new webSite(4, "aws.amazon.com", 200000));
list.add(new webSite(5, "mkyong.com", 1));
list.add(new webSite(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(webSite::getName, webSite::getWebsites));
System.out.println("Result 1 : " + result1);
}
}
输出 - 下面的错误信息有点误导,应该显示“linode”而不是键的值。
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90000
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1245)
//...
解决上述重复的关键问题,请传入第三个mergeFunction参数,如下所示:
Map<String, Long> result1 = list.stream().collect(
Collectors.toMap(webSite::getName, webSite::getWebsites,
(oldValue, newValue) -> oldValue
)
);
尝试newValue
Map<String, Long> result1 = list.stream().collect(
Collectors.toMap(webSite::getName, webSite::getWebsites,
(oldValue, newValue) -> newvalue
)
);
列出Map- 排序Collect
package com.zzg.java8;
import java.util.*;
import java.util.stream.Collectors;
public class TestSortCollect {
public static void main(String[] args) {
List<webSite> list = new ArrayList<>();
list.add(new webSite(1, "liquidweb.com", 80000));
list.add(new webSite(2, "linode.com", 90000));
list.add(new webSite(3, "digitalocean.com", 120000));
list.add(new webSite(4, "aws.amazon.com", 200000));
list.add(new webSite(5, "mkyong.com", 1));
list.add(new webSite(6, "linode.com", 100000));
//example 1
Map result1 = list.stream()
.sorted(Comparator.comparingLong(webSite::getWebsites).reversed())
.collect(
Collectors.toMap(
webSite::getName, webSite::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);
}
}