java-8

How to log filtered values in Java Streams

泪湿孤枕 提交于 2020-12-01 09:13:00
问题 I have a requirement to log/sysout the filtered values in Java Streams. I am able to log/sysout the non-filtered value using peek() method. However, can someone please let me know how to log filtered values? For example, let's say I have a list of Person objects like this: List<Person> persons = Arrays.asList(new Person("John"), new Person("Paul")); I want to filter out those persons who are not "John" as follows: persons.stream().filter(p -> !"John".equals(p.getName())).collect(Collectors

Java 8 extract first key from matching value in a Map

别说谁变了你拦得住时间么 提交于 2020-11-30 04:36:13
问题 Suppose I have a map of given name, surname pairs and I want to find the given name of the first entry in that map that has the surname matching a certain value. How would we do this in a java 8 fashion. In my test case example below I put two ways that would do it. However the first one (looking for the given name of the first person with a surname of "Donkey") will throw java.util.NoSuchElementException: No value present so it is not safe. The second one works but it is not only harder to

Filter an object list based on Integerlist [closed]

北战南征 提交于 2020-11-30 00:12:19
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Filter an object list based on Integerlist [closed]

有些话、适合烂在心里 提交于 2020-11-30 00:12:11
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Filter an object list based on Integerlist [closed]

此生再无相见时 提交于 2020-11-30 00:10:43
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Filter an object list based on Integerlist [closed]

半城伤御伤魂 提交于 2020-11-30 00:10:05
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Filter an object list based on Integerlist [closed]

為{幸葍}努か 提交于 2020-11-30 00:09:09
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Filter an object list based on Integerlist [closed]

狂风中的少年 提交于 2020-11-30 00:09:00
问题 Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 days ago . Improve this question I have a class like class Employee String id String name I want to filter out the list of employees based on the list of deptId which is an integer list Employee emp1 = new Employee("1","Ally"); Employee emp2 = new Employee("2","Billy"); ArrayList<Employee>

Java8: Collect min, max and avg from List to Map [duplicate]

邮差的信 提交于 2020-11-29 02:50:44
问题 This question already has answers here : Find maximum, minimum, sum and average of a list in Java 8 (3 answers) Closed last year . I have List of Integres and want get min, max and avg from list to Map. Below is my code, List<Integer> numbers = Arrays.asList(-2, 1, 2, 3, 5, 6, 7, 8, 9, 10, 100); int min = numbers.stream().mapToInt(n -> n).min().getAsInt(); int max = numbers.stream().mapToInt(n->n).max().getAsInt(); double avg = numbers.stream().mapToInt(n->n).average().getAsDouble(); Map

Concatenate Optional Lists

一笑奈何 提交于 2020-11-28 10:47:35
问题 I have three Optional> which have to be combined and returned. I tried to use Optional.map() and flatmap() but was not successful. public Optional<List<Entiy>> getRecords() { Optional<List<Entiy>> entity1 = repo.findAllByStatus("1"); Optional<List<Entiy>> entity2 = repo.findAllByStatus("2"); Optional<List<Entiy>> entity3 = repo.findAllByStatus("3"); //Need to return a concatenation of entity1, 2 and 3 } Any thoughts on how to do is efficiently? 回答1: Something like : return Optional.of(Stream