引言
几个 Java 示例向您展示如何使用 java8 流 API 过滤 Map。
Java 8 之前:
private static void beforeJava8() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "linode.com");
map.put(2, "heroku.com");
String result = "";
for (Map.Entry<Integer, String> entry : map.entrySet()) {
if("something".equals(entry.getValue())){
result = entry.getValue();
}
}
}
使用 java8,您可以将 Map.entrySet ()转换为流,然后使用 filter ()和 collect ()。
private static void java8() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "linode.com");
map.put(2, "heroku.com");
//Map -> Stream -> Filter -> String
String result = map.entrySet().stream().filter(x -> "something".equals(x.getValue()))
.map(x -> x.getValue()).collect(Collectors.joining());
//Map -> Stream -> Filter -> MAP
Map<Integer, String> collect = map.entrySet().stream().filter(x -> x.getKey() == 2)
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
// or like this
Map<Integer, String> collect1 = map.entrySet().stream().filter(x -> x.getKey() == 3)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
1. java8- 过滤一个 Map
通过值筛选 Map 并返回 String 的完整示例。
private static void one() {
Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com");
// Before Java 8
String result = "";
for (Map.Entry<Integer, String> entry : HOSTING.entrySet()) {
if ("aws.amazon.com".equals(entry.getValue())) {
result = entry.getValue();
}
}
System.out.println("Before Java 8 : " + result);
//Map -> Stream -> Filter -> String
result = HOSTING.entrySet().stream()
.filter(map -> "aws.amazon.com".equals(map.getValue()))
.map(map -> map.getValue())
.collect(Collectors.joining());
System.out.println("With Java 8 : " + result);
// filter more values
result = HOSTING.entrySet().stream().filter(x -> {
if(!x.getValue().contains("amazon") && !x.getValue().contains("digital")) {
return true;
}
return false;
}).map(map -> map.getValue()).collect(Collectors.joining(","));
System.out.println("With Java 8 : " + result);
}
输出:
Before Java 8 : aws.amazon.com
With Java 8 : aws.amazon.com
With Java 8 : linode.com,heroku.com
2.java8- 过滤 Map # 2
这是另一个按键筛选 Map 的示例,但这次将返回一个 Map。
private static void two() {
Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com");
//Map -> Stream -> Filter -> Map
Map<Integer, String> collect = HOSTING.entrySet().stream().filter(map -> map.getKey() == 2)
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
System.out.println(collect);
Map<Integer, String> collect1 = HOSTING.entrySet().stream().filter(map -> map.getKey() <= 3)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
System.out.println(collect1);
}
输出:
{2=heroku.com}
{1=linode.com, 2=heroku.com, 3=digitalocean.com}
3.Java 8-Filter a Map # 3-Predicate
这一次,尝试新的 java8 Predicate。
// 提取公共校验为谓语
public static <K, V> Map<K, V> filterByValue(Map<K, V> map, Predicate<V> predicate) {
return map.entrySet().stream()
.filter(x->predicate.test(x.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}
private static void three() {
Map<Integer, String> HOSTING = new HashMap<>();
HOSTING.put(1, "linode.com");
HOSTING.put(2, "heroku.com");
HOSTING.put(3, "digitalocean.com");
HOSTING.put(4, "aws.amazon.com");
HOSTING.put(5, "aws2.amazon.com");
// {1=linode.com}
Map<Integer, String> filteredMap = filterByValue(HOSTING, x->x.contains("linode"));
System.out.println(filteredMap);
// {1=linode.com, 4=aws.amazon.com, 5=aws2.amazon.com}
Map<Integer, String> filteredMap2 = filterByValue(HOSTING, x -> (x.contains("aws") || x.contains("linode")));
System.out.println(filteredMap2);
// {4=aws.amazon.com}
Map<Integer, String> filteredMap3 = filterByValue(HOSTING, x -> (x.contains("aws") && !x.contains("aws2")));
System.out.println(filteredMap3);
// {1=linode.com, 2=heroku.com}
Map<Integer, String> filteredMap4 = filterByValue(HOSTING, x -> (x.length() <= 10));
System.out.println(filteredMap4);
}
输出:
{1=linode.com}
{1=linode.com, 4=aws.amazon.com, 5=aws2.amazon.com}
{4=aws.amazon.com}
{1=linode.com, 2=heroku.com}
源码见:java-8-demo
系列文章详见:Java 8 教程
文档信息
- 本文作者:Zhang jinmiao
- 本文链接:http://zhangjinmiao.github.io/java8/2019/08/12/Java-8-%E5%AF%B9-map-%E8%BF%87%E6%BB%A4.html
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)