Java8将List转为Map 1、实体
1public class Hosting {
2
3private int id;
4
5private String name;
6
7private long websites;
8
9public Hosting(int id, String name, long websites) {
10this.id = id;
11this.name = name;
12this.websites = websites;
13    }
14
15public int getId() {
16return id;
17    }
18
19public void setId(int id) {
20this.id = id;
21    }
22
23public String getName() {
24return name;
25    }
26
27public void setName(String name) {
28this.name = name;
29    }
30
31public long getWebsites() {
32return websites;
33    }
34
35public void setWebsites(long websites) {
36this.websites = websites;
37    }
38
39    @Override
40public String toString() {
41return "Hosting{" +
42                "id=" + id +
43                ", name='" + name + '\'' +
44                ", websites=" + websites +
45                '}';
46    }
47 }
2、将List转为Map
1public class List2Map {
2
3public static void main(String[] args) {
4        List<Hosting> hostings = new ArrayList<>();
5        hostings.add(new Hosting(1, "liquidweb", 80000));
6        hostings.add(new Hosting(2, "linode", 90000));
7        hostings.add(new Hosting(3, "digitalocean", 120000));
8        hostings.add(new Hosting(4, "aws.amazon", 200000));
9        hostings.add(new Hosting(5, "mkyong", 1));
10
11// key = id, value = websites
12        Map<Integer, String> id2Name = hostings.stream()
13                .Map(Hosting::getId, Hosting::getName));
14        System.out.println("id2Name: " + id2Name);
15
16// key = name, value = websites
17        Map<String, Long> name2Websites = hostings.stream()
18                .Map(Hosting::getName, Hosting::getWebsites));
19        System.out.println("name2Websites: " + name2Websites);
20
21// key = id, value = websites
22        Map<Integer, String> id2NamDifferent = hostings.stream()
23                .Map(h -> h.getId(), h -> h.getName()));
24        System.out.println("id2NamDifferent: " + id2NamDifferent);
25
26    }
27 }
3、将List转为Map(重复key的情况)
public class List2MapDuplicatedKey {
public static void main(String[] args) {
List<Hosting> hostings = new ArrayList<>();
hostings.add(new Hosting(1, "liquidweb", 80000));
hostings.add(new Hosting(2, "linode", 90000));
hostings.add(new Hosting(3, "digitalocean", 120000));
hostings.add(new Hosting(4, "aws.amazon", 200000));
hostings.add(new Hosting(5, "mkyong", 1));
hostings.add(new Hosting(6, "linode", 100000)); // 重复的key
// key = name, vaule = websites
Map<String, Long> name2Websites = hostings.stream()
.
Map(Hosting::getName, Hosting::getWebsites));
System.out.println("name2Websites: " + name2Websites);
}
}
在上⾯⼀段代码中,"linbode"做为key被add两次,那么在转为map过程会发⽣什么?如下:如何解决重复key的情况?只需要在16⾏加⼊如下处理即可:
1public class List2MapDuplicatedKey {
2
3public static void main(String[] args) {
4        List<Hosting> hostings = new ArrayList<>();
5        hostings.add(new Hosting(1, "liquidweb", 80000));
6        hostings.add(new Hosting(2, "linode", 90000));
7        hostings.add(new Hosting(3, "digitalocean", 120000));
8        hostings.add(new Hosting(4, "aws.amazon", 200000));
9        hostings.add(new Hosting(5, "mkyong", 1));
10
11        hostings.add(new Hosting(6, "linode", 100000)); // 重复的key
12
13// key = name, vaule = websites
14        Map<String, Long> name2Websites = hostings.stream()
15                .Map(Hosting::getName, Hosting::getWebsites,
16                        (oldValue, newValue) -> newValue));
17        System.out.println("name2Websites: " + name2Websites);
18
19    }
20 }
4、将List转为Map并排序
1public class List2MapWithSort {
sortedlist2
3public static void main(String[] args) {
4        List<Hosting> hostings = new ArrayList<>();
5        hostings.add(new Hosting(1, "liquidweb", 80000));
6        hostings.add(new Hosting(2, "linode", 90000));
7        hostings.add(new Hosting(3, "digitalocean", 120000));
8        hostings.add(new Hosting(4, "aws.amazon", 200000));
9        hostings.add(new Hosting(5, "mkyong", 1));
10        hostings.add(new Hosting(6, "linode", 100000));
11
12// key = name, vaule = websites
13        Map<String, Long> name2Websites = hostings.stream()
14                .sorted(Comparatorparing(Hosting::getWebsites).reversed())
15                .Map(Hosting::getName, Hosting::getWebsites,
16                        (oldValue, newValue) -> newValue,  // 如果有相同的key,使⽤新key
17                        LinkedHashMap::new));  // 返回ListedHashMap,保持有序
18
19        System.out.println("name2Websites: " + name2Websites);
20    }
21 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。