Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering on Java 8 List

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

enter image description here

According to documentation, List has a filter method and this should work http://www.functionaljava.org/examples-java8.html

What am I missing?

Thanks

like image 764
Nir Ben Yaacov Avatar asked Dec 23 '22 21:12

Nir Ben Yaacov


2 Answers

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);
like image 124
Naman Avatar answered Dec 26 '22 12:12

Naman


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());
like image 23
azro Avatar answered Dec 26 '22 10:12

azro