I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is 1 and only 1 match to the filter criteria.
Take this code:
public static void main(String[] args) {
LinkedList<User> users = new LinkedList<>();
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
User match = users.stream().filter((user) -> user.getId() == 1).findAny().get();
System.out.println(match.toString());
}
static class User {
@Override
public String toString() {
return id + " - " + username;
}
int id;
String username;
public User() {
}
public User(int id, String username) {
this.id = id;
this.username = username;
}
public void setUsername(String username) {
this.username = username;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public int getId() {
return id;
}
}
This code finds a User based on their ID. But there are no guarantees how many Users matched the filter.
Changing the filter line to:
User match = users.stream().filter((user) -> user.getId() < 0).findAny().get();
Will throw a NoSuchElementException (good!)
I would like it to throw an error if there are multiple matches, though. Is there a way to do this?
|
|
|||
|
|
Ok, thanks @ZouZou. I wasn't entirely certain what that method did. Why is there no
Stream::size ? – ryvantage Mar 27 at 17:44
|
||
|
@ryvantage Because a stream can only be used once: calculating its size means "iterating" over it and after that you can't use the stream any longer. – assylias Mar 27 at 17:45
|
||
|
|
Wow. That one comment helped me understand
Streams so much more than I did before... – ryvantage Mar 27 at 17:50
|
|
13
|
Technically there's an ugly 'workaround' that involves What I do in these cases is just collecting it in a list, like this:
I am not aware of a way to do this in the API, meanwhile I will work on another example involving a custom element. Update, You should create your own
What it does is:
Used as:
You can then customize this New update, I revised my old answer once more for
|
||||||||||||||||
add a comment
|
|
9
|
Wow, such complexity! :-) 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:
Then verify the size of the result list. |
||||||||
add a comment
|
|
6
|
You could roll your own Collector for this:
...or using your own |
||||||||||||||||||||
add a comment
|
|
5
|
The "escape hatch" operation that lets you do weird things that are not otherwise supported by streams is to ask for an
Guava has a convenience method to take an |
|||
|
add a comment
|
|
3
|
The exception is thrown by
which throws a Or you could use a reduction combined with an optional:
The reduction essentially returns:
The result is then wrapped in an optional. But the simplest solution would probably be to just collect to a collection, check that its size is 1 and get the only element. |
||||||||
add a comment
|
|
0
|
Have you tried this
|
||||
add a comment
|
count()is a terminal operation so you can't do that. The stream can't be used after. – ZouZou Mar 27 at 17:42