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並排序

 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM