-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSkipItemsByCondition.java
More file actions
37 lines (32 loc) · 962 Bytes
/
SkipItemsByCondition.java
File metadata and controls
37 lines (32 loc) · 962 Bytes
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
package by.andd3dfx.common;
import java.util.Iterator;
import java.util.Map;
/**
* <pre>
* We have an interface:
* public interface Condition<K> {
* boolean check(K key);
* }
*
* Implement class with method to filter items in map in case if key satisfy to check(K key) method.
* </pre>
*/
public class SkipItemsByCondition {
public interface Condition<K> {
boolean check(K key);
}
public <K, V> Map<K, V> filterUsingIterator(Map<K, V> map, Condition<K> condition) {
Iterator<K> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
K key = iterator.next();
if (condition.check(key)) {
iterator.remove();
}
}
return map;
}
public <K, V> Map<K, V> filterUsingRemoveIf(Map<K, V> map, Condition<K> condition) {
map.keySet().removeIf(condition::check);
return map;
}
}