Reputation: 1182
I have a list of type List in functional java using List type provided by fj.data.List
import fj.data.List
List<Long> managedCustomers
I am trying to filter it using the following:
managedCustomers.filter(customerId -> customerId == 5424164219L)
I get this message
According to documentation, List has a filter method and this should work http://www.functionaljava.org/examples-java8.html
What am I missing?
Thanks
Upvotes: 4
Views: 2443
Reputation: 54148
What you did seem a bit weird, Streams
(to use filter
) are commonly used like this (I don't know what you really want to do with the filtrate list, you can tell me in the comment tp get a more precise answer) :
//Select and print
managedCustomers.stream().filter(customerId -> customerId == 5424164219L)
.forEach(System.out::println);
//Select and keep
ArrayList<> newList = managedCustomers.stream().filter(customerId -> customerId == 5424164219L)
.collect(Collectors.toList());
Upvotes: 4
Reputation: 31888
As already pointed out in the comment by @Alexis C
managedCustomers.removeIf(customerId -> customerId != 5424164219L);
should get you the filtered list if the customerId
equals 5424164219L
.
Edit - The above code modifies the existing managedCustomers
removing the other entries. And also the other way to do so is using the stream().filter()
as -
managedCustomers.stream().filter(mc -> mc == 5424164219L).forEach(//do some action thee after);
Edit 2 -
For the specific fj.List
, you can use -
managedCustomers.toStream().filter(mc -> mc == 5424164219L).forEach(// your action);
Upvotes: 5
Reputation: 533530
A lambda determines it's types by context. When you have a statement which doesn't compile, the javac
sometimes gets confused and complains your lambda won't compile when the real reason is you have made some other mistake which is why it can't workout what the type of your lambda should be.
In this case, there is no List.filter(x)
method, which is the only error you should see because unless you fix that your lambda is never going to make sense.
In this case, rather than use filter, you could use anyMatch as you already know there is only one possible value where customerId == 5424164219L
if (managedCustomers.stream().anyMatch(c -> c == 5424164219L) {
// customerId 5424164219L found
}
Upvotes: 1