2016-07-20 6 views
1

このメソッドでは、この値を印刷する必要はありません。返す必要がありますforEachから値を返す方法

public void showStoreProductQuantity(){ 

     productListQuantity.entrySet().stream().forEach((Map.Entry<Product, Integer> entry) -> { 
     Product product = (Product) entry.getKey(); 
     System.out.print(product); 
     Integer quantity = entry.getValue(); 
     System.out.println(" Quantity is:"+quantity); 
     }); 

} 

どうすればいいですか?

+0

整数配列を使用してこの値を返すことができます – Shubh

+3

return productListQuantity.values(); ' – Wernsey

答えて

0

私はあなたが正確に必要なものはかなりわからないんだけど、あなたはListにマップの値を収集する必要がある場合、あなたは適切なCollector.map()たものと.collect()彼らにあります:

return productListQuantity.entrySet().stream() 
          .map(Map.Entry::getValue) 
          .collect(Collectors.toList()); 

public List<MapValueType> showStoreProductQuantity() { } 

MapValueTypeがあなたのproductListQuantity内の値の種類です:もちろん

は、あなたがするメソッドのシグネチャを変更する必要があります。

+0

番号:7価格:91説明-3名前-4数量:70 番号:1価格:23説明5名前: 6 Quantity:10 – Verlen

+0

次に、 'Entry'要素をマッピングする方法で少し演奏しなければなりません。 –

関連する問題