Filter Java Stream to 1 and only 1 element
The other answers that involve writing a custom Collector
are probably more efficient (such as Louis Wasserman's, +1), but if you want brevity, I'd suggest the following:
List<User> result = users.stream()
.filter(user -> user.getId() == 1)
.limit(2)
.collect(Collectors.toList());
Then verify the size of the result list.
if (result.size() != 1) {
throw new IllegalStateException("Expected exactly one user but got " + result);
User user = result.get(0);
}
Create a custom Collector
public static <T> Collector<T, ?, T> toSingleton() {
return Collectors.collectingAndThen(
Collectors.toList(),
list -> {
if (list.size() != 1) {
throw new IllegalStateException();
}
return list.get(0);
}
);
}
We use Collectors.collectingAndThen
to construct our desired Collector
by
- Collecting our objects in a
List
with theCollectors.toList()
collector. - Applying an extra finisher at the end, that returns the single element — or throws an
IllegalStateException
iflist.size != 1
.
Used as:
User resultUser = users.stream()
.filter(user -> user.getId() > 0)
.collect(toSingleton());
You can then customize this Collector
as much as you want, for example give the exception as argument in the constructor, tweak it to allow two values, and more.
An alternative — arguably less elegant — solution:
You can use a 'workaround' that involves peek()
and an AtomicInteger
, but really you shouldn't be using that.
What you could do instead is just collecting it in a List
, like this:
LinkedList<User> users = new LinkedList<>();
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
List<User> resultUserList = users.stream()
.filter(user -> user.getId() == 1)
.collect(Collectors.toList());
if (resultUserList.size() != 1) {
throw new IllegalStateException();
}
User resultUser = resultUserList.get(0);
For the sake of completeness, here is the ‘one-liner’ corresponding to @prunge’s excellent answer:
User user1 = users.stream()
.filter(user -> user.getId() == 1)
.reduce((a, b) -> {
throw new IllegalStateException("Multiple elements: " + a + ", " + b);
})
.get();
This obtains the sole matching element from the stream, throwing
NoSuchElementException
in case the stream is empty, orIllegalStateException
in case the stream contains more than one matching element.
A variation of this approach avoids throwing an exception early and instead represents the result as an Optional
containing either the sole element, or nothing (empty) if there are zero or multiple elements:
Optional<User> user1 = users.stream()
.filter(user -> user.getId() == 1)
.collect(Collectors.reducing((a, b) -> null));