-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterMap.java
More file actions
69 lines (58 loc) · 2.44 KB
/
FilterMap.java
File metadata and controls
69 lines (58 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package map;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class FilterMap {
public static Map<Integer, String> hosting = new HashMap<>();
static {
hosting.put(1, "linode.com");
hosting.put(2, "heroku.com");
hosting.put(3, "digitalocean.com");
hosting.put(4, "aws.amazon.com");
}
public static void main(String[] args) {
filterMapReturnString();
filterMapReturnMap();
predicate();
}
private static void filterMapReturnString() {
String result = hosting.entrySet().stream().filter(map-> "linode.com".equals(map.getValue())).map(map -> map.getValue()).collect(Collectors.joining());
System.out.println(result);
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("<-------------------------------------->");
System.out.println(result);
}
private static void filterMapReturnMap() {
Map<Integer,String> collect = hosting.entrySet().stream().filter(map -> map.getKey() == 2).collect(Collectors.toMap(p->p.getKey(), p -> p.getValue()));
print(collect);
Map<Integer, String> collect2 = hosting.entrySet().stream().filter(map -> map.getKey() <=3).collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
print(collect2);
}
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 predicate() {
System.out.println("<------------------PREDICATE------------------->");
Map<Integer, String> filteredMap = filterbyValue(hosting, x->x.contains("linode"));
filteredMap.forEach((k,v)->{
System.out.println(k + " " + v);
});
System.out.println("<------------------PREDICATE------------------->");
Map<Integer, String> filteredMap1 = filterbyValue(hosting, x->(x.length() <= 10));
filteredMap1.forEach((k,v)->{
System.out.println(k + " " + v);
});
}
private static void print(Map map) {
System.out.println("<------------------MAP------------------->");
map.forEach((k,v)->{
System.out.println(k + " " + v);
});
System.out.println("<------------------MAP------------------->");
}
}