ksl*_*ksl 3 java foreach lambda java-8
Java 8 allows iteration over a Map using forEach and a lambda expression as follows:
myMap.forEach((k, v)->{ System.out.println("Key: " + k + " Value: " + v); });
Run Code Online (Sandbox Code Playgroud)
Is it possible to iterate over a MultivaluedMap using forEach and a lambda expression?
UDPATE
How do I call foo with 2 String parameters for a MultivaluedMap<String, String>?
myMultiMap.forEach((k, v)->{ foo(k, v); });
Run Code Online (Sandbox Code Playgroud)
The interface MultivaluedMap<K, V> extends the Map<K,List<V>> interface, therefore, there is forEach method and that is possible to use it.
new MultivaluedHashMap<String, String>()
.forEach((String key, List<String> list)-> { ... });
Run Code Online (Sandbox Code Playgroud)
I don't know what your foo method does, but I suggest* considering my point about that:
public <K, V> void foo(K key, V... values) { ... }
Run Code Online (Sandbox Code Playgroud)
In such case, you needn't write inner forEach inside the lambda.
*(it is wrong as explained @Holder in the comments)
So, there is the only one proper way:
map.forEach((k, l) -> l.forEach( v -> foo(k, v)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8801 次 |
| 最近记录: |