Java 8 Stream to find element in list
问题 I have the following class: public class Item { int id; String name; // few other fields, contructor, getters and setters } I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams. public void foobar() { List<Item> items = getItemList(); List<Integer> ids = getIdsToLookup(); int id, i = ids.size() - 1; while (i >= 0) { id = ids.get(i); Optional<Item> item = items .stream() .filter(a -> a.getId() == id)