【JAVA】由一個將JSONArray轉成Map的需求引發的lambda語法的學習


在寫代碼時總是遇到將JSONArray轉成Map的需求,想要用java8的lambda表達式去編寫,發現網上沒有類似的參考答案,無果自己耐心的學了下函數式編程,完美解決了這個問題
網上大多數代碼都是這樣的,截取片段如下

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity()));
}

一、問題

針對的是List其元素為對象的轉換,不符合自我需求,JSONArray 元素是Object

二、解決

public void testStream(){
        JSONObject ecsInstanceList = productInstance.getEcsInstanceList();
        JSONArray ecsArr = ecsInstanceList.getJSONObject("data").getJSONArray("result") ;
        
        Map<String, Object> collect = ecsArr.stream().collect(Collectors.toMap(i -> {JSONObject a= (JSONObject)i;String k = a.getString("instanceId");return k;} , Function.identity()));
        System.out.println(collect);
    }

三、解釋

  • 3.1、 ecsArr.stream() 是創建一個stream流
  • 3.2、緊跟着是collect(Collector<? super T, A, R> collector) 參數是一個收集器
  • 3.3、收集器的創建是用Collectors 提供的靜態方法.toMap會創建一個map收集器
  • 3.4、重點是toMap方法參數是兩個函數式接口,一個key的函數式接口,一個是value的函數式接口,看源碼就容易理解了
 * @param <T> the type of the input elements
 * @param <K> the output type of the key mapping function
 * @param <U> the output type of the value mapping function
 * @param keyMapper a mapping function to produce keys
 * @param valueMapper a mapping function to produce values
public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper) {
        return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
    }
  • 3.5 其中容易迷惑的是 Function.identity(),一開始不清楚到底啥意思,同樣看源碼
static <T> Function<T, T> identity() {
        return t -> t;
    }

這樣看你會發現它是靜態方法,所以是Function.的方式創建,這個方法將返回一個函數式接口,接口特殊之處在於t -> t,意思就是輸入什么內容輸出便是什么內容,這樣Function.identity() 參數也可以換成 v -> v,一個道理


免責聲明!

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



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