Access to the fields of all objects in the list

I have List<Foo>where Foo- this classis containing a field Stringwith a name name. I want to know whether the Listobject Foowith him nameequal "bar".

Is this possible without repeating each object and testing one at a time?

I am using Java 8.

+4
source share
1 answer

You can use a parallel stream to return Optional<String>, which may or may not be empty, depending on whether a bar exists as an attribute for an object Fooin List<Foo>:

list.parallelStream()
    .map(Foo::getName)
    .filter(s -> s.equals("bar"))
    .findAny();

List<Foo>, O (n/# )

- name, , , , , .

+2

Source: https://habr.com/ru/post/1681142/


All Articles