2016-05-30 63 views
1

我想將我的Hashmap for循環轉換爲Java8流。這是我的代碼。Java streaming Hashmap

int amount = 100; 
Map<String, Account> map = accountMap; 

for (Map.Entry<String, Account> entry : map.entrySet()) { 
    String id = entry.getValue().getAccount_id(); 
    updateAccount(amount, id); 
} 

它會是這樣,但想不出確切的語法:

accountMap.entrySet().parallelStream() 
        .collect(Collectors.toMap(
          e -> e.getKey(), 
          e -> e.getValue() 
          //Where can I call external method? 
        )); 
+2

'map.values()的forEach(V? - > changeAmount(金額,v.getAccount_id())'但是我會保持你的循環只是,遍歷所有的值,而不是條目集。因爲你不想要密鑰 – Tunaki

+0

@Tunaki多數民衆贊成在每一個而不是並行流? – Spring

+0

@Tunaki我看到,但我想使用並列從多線程利用 – Spring

回答

3

既然你只使用值獲得的值,並創建了一條小溪。

map.values().parallelStream() 
       .forEach(value -> 
       updateAccount(amount, value.getAccount_id()));