I'm used to C# and the lambda expressions, but i couldn't filter a java list based on certain criterias.
Ex:
List<MyObject> list;
I need to get all the MyObject that have a isMyFlag() true.
In C# is really easy using IEnumerable... Is there any similar to IEnumerable in Java?
See the new Stream API in Java 8
You need to do a filter and then collect the results in a list using the Stream API:
list.stream().filter(x -> x.isMyFlag()).collect(Collectors.toList());
With lambdas you could do the following:
List<MyObject> filteredList = list.stream().filter(myObj -> myObj.isMyFlag())
.collect(Collectors.toList());
To use the Stream API you need to call the stream() method. This is perhaps a surprise coming from C# as you don't need to do this, but once you have got over that hurdle it should be much the same.
List<MyObject> list2 = list.stream()
.filter(m -> m.isMyFlag())
.collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.