国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

這兩個函數(shù)如何用java8 新api改為one-liner
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-04-17 13:11:45
0
1
328

方法都很簡單, 不解釋了. 如何用java8 的新api改為更優(yōu)雅的 一行代碼格式?

void addOne(Map<String, Integer> map, 
        String key, 
        int increment, 
        int defVal){
    if(map.containsKey(key))
        map.put(key, map.get(key) + increment);
    else
        map.put(key, defVal);
}

void addString(Map<String, Set<String>> map, 
               String key, 
               String val){
    if(!map.containsKey(key))
        map.put(key, new HashSet<>());
    map.get(key).add(val);
}
PHP中文網(wǎng)
PHP中文網(wǎng)

認(rèn)證高級PHP講師

reply all(1)
Ty80

It should be possible to use several tool methods provided by the Map interface, as follows:

void addOne(Map<String, Integer> map, 
        String key, 
        int increment, 
        int defVal){
    // map.compute(key, (k, v) -> key.equals(k) ? v + increment : defVal);
    map.compute(key, (k, v) -> v == null ? defVal : v + increment)
}

void addString(Map<String, Set<String>> map, 
               String key, 
               String val){
    map.computeIfAbsent(key, k -> new HashSet<>()).add(val);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template