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;}
}
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
我试图找到一个聪明的方法来grep其值大于X的所有键
例如,如果X == 3,它应该返回橙子,西瓜和猕猴桃
显而易见的方法是迭代地图并比较值,但有没有简单,简洁的方法呢?
Streams,是的。使用
expirationMap.entrySet().stream()
.filter(entry -> entry.getValue() > 3L)
.map(Entry::getKey)
.collect(Collectors.toList());
获取密钥列表。
我们需要流式传输映射条目而不仅仅是值或键,因为我们需要比较一个(值)并返回另一个(键)。好的,不需要,如评论中的零点所示。
filter
方法获取值并将其与3进行比较,丢弃不大于3的元素;然后我们使用map
方法将条目映射到它们的值。最后,我们将结果收集到List
中。
使用set和key lookup略有不同的变化:
Set<String> greppedKeys = expirationMap.keySet().stream() // keyset only
.filter(key -> expirationMap.get(key) > 3L) // X here being 3L
.collect(Collectors.toSet()); // all keys would be unique anyway
这将使用流做的技巧:
expirationMap.entrySet().stream().filter(e -> e.getValue() > 3)
.forEach(e -> {
System.out.println("key " + e.getKey() + "value" + e.getValue());
});
请参阅下面的另一种可读方法。
expirationMap.forEach((key, value) -> {
if (value > x) {
System.out.println(format("key: %s, value: %s", key, value));
}
});
.forEach部分将遍历map的entrySet()并分别将每个条目的键和值提取到(key,value)。
除了其他人所做的另一种方法可以是使用Interface Predicate<T>
,其默认方法test(T t)
可用于评估大于3的值。
Predicate<Long> filterGreater3 = f-> f>3;
expirationMap.entrySet()
.stream()
.filter(x->filterGreater3.test(x.getValue()))
.forEach(System.out::println);