Java8将List转为Map

1、实体

 1 public class Hosting {
 2 
 3     private int id;
 4 
 5     private String name;
 6 
 7     private long websites;
 8 
 9     public Hosting(int id, String name, long websites) {
10         this.id = id;
11         this.name = name;
12         this.websites = websites;
13     }
14 
15     public int getId() {
16         return id;
17     }
18 
19     public void setId(int id) {
20         this.id = id;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 
31     public long getWebsites() {
32         return websites;
33     }
34 
35     public void setWebsites(long websites) {
36         this.websites = websites;
37     }
38 
39     @Override
40     public String toString() {
41         return "Hosting{" +
42                 "id=" + id +
43                 ", name='" + name + '\'' +
44                 ", websites=" + websites +
45                 '}';
46     }
47 }

2、将List转为Map

 1 public class List2Map {
 2 
 3     public static void main(String[] args) {
 4         List<Hosting> hostings = new ArrayList<>();
 5         hostings.add(new Hosting(1, "liquidweb.com", 80000));
 6         hostings.add(new Hosting(2, "linode.com", 90000));
 7         hostings.add(new Hosting(3, "digitalocean.com", 120000));
 8         hostings.add(new Hosting(4, "aws.amazon.com", 200000));
 9         hostings.add(new Hosting(5, "mkyong.com", 1));
10 
11         // key = id, value = websites
12         Map<Integer, String> id2Name = hostings.stream()
13                 .collect(Collectors.toMap(Hosting::getId, Hosting::getName));
14         System.out.println("id2Name: " + id2Name);
15 
16         // key = name, value = websites
17         Map<String, Long> name2Websites = hostings.stream()
18                 .collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites));
19         System.out.println("name2Websites: " + name2Websites);
20 
21         // key = id, value = websites
22         Map<Integer, String> id2NamDifferent = hostings.stream()
23                 .collect(Collectors.toMap(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.com", 80000));
        hostings.add(new Hosting(2, "linode.com", 90000));
        hostings.add(new Hosting(3, "digitalocean.com", 120000));
        hostings.add(new Hosting(4, "aws.amazon.com", 200000));
        hostings.add(new Hosting(5, "mkyong.com", 1));

        hostings.add(new Hosting(6, "linode.com", 100000)); // 重复的key

        // key = name, vaule = websites
        Map<String, Long> name2Websites = hostings.stream()
                .collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites));
        System.out.println("name2Websites: " + name2Websites);

    }
}

在上面一段代码中,"linbode.com"做为key被add两次,那么在转为map过程会发生什么?如下:

如何解决重复key的情况?只需要在16行加入如下处理即可:

 1 public class List2MapDuplicatedKey {
 2 
 3     public static void main(String[] args) {
 4         List<Hosting> hostings = new ArrayList<>();
 5         hostings.add(new Hosting(1, "liquidweb.com", 80000));
 6         hostings.add(new Hosting(2, "linode.com", 90000));
 7         hostings.add(new Hosting(3, "digitalocean.com", 120000));
 8         hostings.add(new Hosting(4, "aws.amazon.com", 200000));
 9         hostings.add(new Hosting(5, "mkyong.com", 1));
10 
11         hostings.add(new Hosting(6, "linode.com", 100000)); // 重复的key
12 
13         // key = name, vaule = websites
14         Map<String, Long> name2Websites = hostings.stream()
15                 .collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites,
16                         (oldValue, newValue) -> newValue));
17         System.out.println("name2Websites: " + name2Websites);
18 
19     }
20 }

4、将List转为Map并排序

扫描二维码关注公众号,回复: 5554979 查看本文章
 1 public class List2MapWithSort {
 2 
 3     public static void main(String[] args) {
 4         List<Hosting> hostings = new ArrayList<>();
 5         hostings.add(new Hosting(1, "liquidweb.com", 80000));
 6         hostings.add(new Hosting(2, "linode.com", 90000));
 7         hostings.add(new Hosting(3, "digitalocean.com", 120000));
 8         hostings.add(new Hosting(4, "aws.amazon.com", 200000));
 9         hostings.add(new Hosting(5, "mkyong.com", 1));
10         hostings.add(new Hosting(6, "linode.com", 100000));
11 
12         // key = name, vaule = websites
13         Map<String, Long> name2Websites = hostings.stream()
14                 .sorted(Comparator.comparing(Hosting::getWebsites).reversed())
15                 .collect(Collectors.toMap(Hosting::getName, Hosting::getWebsites,
16                         (oldValue, newValue) -> newValue,   // 如果有相同的key,使用新key
17                         LinkedHashMap::new));   // 返回ListedHashMap,保持有序
18 
19         System.out.println("name2Websites: " + name2Websites);
20     }
21 }

猜你喜欢

转载自www.cnblogs.com/fengkunangel/p/10545137.html
今日推荐