eal*_*eon 6 java hashmap java-8 java-stream
import java.util.*;
import static java.lang.String.format;
public class Dumpground {
private static final String[] fruits = new String[]{"apples", "bananas", "grapes", "oranges", "watermelons", "kiwis"};
static Map<String, Long> expirationMap;
public static void main(String[] args) {
long expiration = 1L;
expirationMap = new HashMap<>();
for (String fruit : values()){
expirationMap.put(fruit, expiration);
expiration++;
}
for (Map.Entry<String, Long> item : expirationMap.entrySet()) {
String key = item.getKey();
Long value = item.getValue();
System.out.println(format("key: %s, value: %s", key, value));
}
}
public static String[] values() {return fruits;}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
key: oranges, value: 4
key: watermelons, value: 5
key: kiwis, value: 6
key: bananas, value: 2
key: apples, value: 1
key: grapes, value: 3
Run Code Online (Sandbox Code Playgroud)
我试图找到一个聪明的方法来grep其值大于X的所有键
例如,如果X == 3,它应该返回橙子,西瓜和猕猴桃
显而易见的方法是迭代地图并比较值,但有没有简单,简洁的方法呢?
Streams,是的.使用
expirationMap.entrySet().stream()
.filter(entry -> entry.getValue() > 3L)
.map(Entry::getKey)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
获取密钥列表.
我们需要流式传输映射条目而不仅仅是值或键,因为我们需要比较一个(值)并返回另一个(键).好的,不需要,如评论中的零点所示.
该filter
方法获取该值并将其与3进行比较,丢弃不大于3的元素; 然后我们使用该map
方法将条目映射到它们的值.最后,我们将结果收集到一个List
.