java-stream

Java 8 Stream to find element in list

喜夏-厌秋 提交于 2020-12-29 05:52:25
问题 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)

Java 8 Stream to find element in list

夙愿已清 提交于 2020-12-29 05:51:33
问题 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)

Java 8 Stream to find element in list

依然范特西╮ 提交于 2020-12-29 05:51:09
问题 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)

Java how to use stream map to return boolean [duplicate]

强颜欢笑 提交于 2020-12-26 06:45:06
问题 This question already has an answer here : Java8 Effectively Final compile time error on non final variable (1 answer) Closed 3 years ago . I am trying to return a boolean for the result. public boolean status(List<String> myArray) { boolean statusOk = false; myArray.stream().forEach(item -> { helpFunction(item).map ( x -> { statusOk = x.status(); // x.status() returns a boolean if (x.status()) { return true; } return false; }); }); } It's complaining variable used in lambda expression should

Error when collecting IntStream to map

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-26 06:36:02
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

Error when collecting IntStream to map

柔情痞子 提交于 2020-12-26 06:32:06
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

Error when collecting IntStream to map

不问归期 提交于 2020-12-26 06:31:29
问题 The following code String[] values = ... .... Map<String, Object> map = new HashMap<>(); for (int i = 0; i < values.length; i++) { map.put("X" + i, values[i]); } is converted by IntelliJ to: Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i], (a, b) -> b)); which can be shortened to Map<String, Object> map = IntStream.range(0, values.length) .collect(Collectors.toMap( i -> "X" + i, i -> values[i])); The 2 stream versions don't

How to apply sorting and limiting after groupby using Java streams

纵然是瞬间 提交于 2020-12-26 04:24:11
问题 I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department. public class Employee { private int id; private String name; private String dept; private int salary; //setters and getters } List<Employee> listOfEmp = new ArrayList<>(); listOfEmp.add(new Employee (1, "A", "IT",100)); listOfEmp.add(new Employee (2, "B", "IT",200)); listOfEmp.add(new Employee (3, "C", "SUPPORT",100));

How to apply sorting and limiting after groupby using Java streams

老子叫甜甜 提交于 2020-12-26 04:23:50
问题 I have the following list of Employee data which I need to group based on the employee department and then I want to find the 2 highest-paid employees in each department. public class Employee { private int id; private String name; private String dept; private int salary; //setters and getters } List<Employee> listOfEmp = new ArrayList<>(); listOfEmp.add(new Employee (1, "A", "IT",100)); listOfEmp.add(new Employee (2, "B", "IT",200)); listOfEmp.add(new Employee (3, "C", "SUPPORT",100));

Java stream order of processing

可紊 提交于 2020-12-25 10:44:38
问题 I have the following: LinkedList<Integer> ints = new LinkedList(); //fill it with some ints Stream<Integer> stream = ints.stream(); //process the stream in some way My question is if it's guaranteed that the order of processing of the stream is as of the underlying LinkedList ? I read the documentation but there was no any info about the ordering. In my case it's critically to preserve the order. 回答1: From the documentation: Streams may or may not have a defined encounter order. Whether or