Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapStream extensions - anyMatch, allMatch, noneMatch with BiPredicate #1832

Merged
merged 2 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -546,6 +547,44 @@ public Optional<Map.Entry<K, V>> maxValues(Comparator<? super V> comparator) {
return underlying.max((e1, e2) -> comparator.compare(e1.getValue(), e2.getValue()));
}

//-----------------------------------------------------------------------
/**
* Returns whether any elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether any of the entries matched the predicate
*/
public boolean anyMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
}


/**
* Returns whether all elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether all of the entries matched the predicate
*/
public boolean allMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.allMatch(e -> predicate.test(e.getKey(), e.getValue()));
}

/**
* Returns whether no elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether none of the entries matched the predicate
*/
public boolean noneMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.noneMatch(e -> predicate.test(e.getKey(), e.getValue()));
}

//-------------------------------------------------------------------------
/**
* Returns an immutable map built from the entries in the stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,25 @@ public void maxValues() {
assertThat(result).isEqualTo(entry("four", 4));
}

//-----------------------------------------------------------------------
public void anyMatch() {
assertThat(MapStream.of(map).anyMatch((key, value) -> key.length() + value < 10)).isTrue();
assertThat(MapStream.of(map).anyMatch((key, value) -> key.length() + value < 8)).isTrue();
assertThat(MapStream.of(map).anyMatch((key, value) -> key.length() + value < 4)).isFalse();
}

public void allMatch() {
assertThat(MapStream.of(map).allMatch((key, value) -> key.length() + value < 10)).isTrue();
assertThat(MapStream.of(map).allMatch((key, value) -> key.length() + value < 8)).isFalse();
assertThat(MapStream.of(map).allMatch((key, value) -> key.length() + value < 4)).isFalse();
}

public void noneMatch() {
assertThat(MapStream.of(map).noneMatch((key, value) -> key.length() + value < 10)).isFalse();
assertThat(MapStream.of(map).noneMatch((key, value) -> key.length() + value < 8)).isFalse();
assertThat(MapStream.of(map).noneMatch((key, value) -> key.length() + value < 4)).isTrue();
}

//-----------------------------------------------------------------------
public void forEach() {
HashMap<Object, Object> mutableMap = new HashMap<>();
Expand Down