java-stream

if-else condition using java 8 stream [duplicate]

一世执手 提交于 2019-12-24 02:03:14
问题 This question already has answers here : How to use if-else logic in Java 8 stream forEach (4 answers) Closed last year . Scenario: There is a situation where I need to set some values to List of objects based on some field condition using the Java 8 streams API. Below is the sample of object User . public class User{ private int id; private String name; private String text; private boolean isActive; } Here is the code I have worked out List<User> users = userDao.getAllByCompanyId(companyId);

Java 8 - filter collection using many filters [duplicate]

一笑奈何 提交于 2019-12-24 01:44:30
问题 This question already has answers here : How to apply multiple predicates to a java.util.Stream? (4 answers) Closed 3 years ago . I would like to filter out my collection using multiple filters. Let's assume I have a list of Strings and a function filter() to filter out empty Strings. List<String> myList = ....... Typically, I would use streams like this: myList.stream() .filter(elem -> filterOut(elem)) .collect(Collectors.toList()); How to apply multiple filters from a collection ( List or

Possible side effect of Stream.peek changing state and why not use it like this

安稳与你 提交于 2019-12-24 00:48:39
问题 A solution that I came up on another Stackoverflow question that is using Stream.peek operation works but still seems like is not right because it mutates state in the Stream.peek method. While researching (here and here) on Stream.peek usage whether it is ok to mutate state I am still not fully convinced that Stream.peek should not mutate state (including state of collection that is source of the Stream ). Here is what Javadoc says: This method exists mainly to support debugging, where you

Does Stream.boxed() preserve order?

落爺英雄遲暮 提交于 2019-12-24 00:27:39
问题 Assume I have an infinite Stream. IntStream istream = IntStream.iterate(0, i -> i + 1).limit(100); Stream<Integer> boxedStream = istream.boxed(); Does the boxed() method preserve order? Probably yes, but I cannot find it in the documentation. 回答1: Actually every intermediate operation preserves an order by default. The only exceptions are: unordered() which removes the ordering constraint. sorted() which changes the order. When it's not explicitly specified, you can assume that operation

How do aggregate operations work on Java streams?

浪尽此生 提交于 2019-12-24 00:26:55
问题 In the following excerpt from the Java tutorials in aggregate operations, we map the names of the people to their sexes. Map<Person.Sex, List<String>> namesByGender = roster .stream() .collect( Collectors.groupingBy( Person::getGender, Collectors.mapping( Person::getName, Collectors.toList()))); I understand that the collect operation: 1) Groups each Person in the stream by the result of getGender. 2) Maps each Person to the result of getName. 3) Forms a list from the results and. 4)

How to count the number of trailing zeroes in an integer using Java 8 Stream/Lambda?

让人想犯罪 __ 提交于 2019-12-23 22:57:56
问题 How to count the number of trailing zeroes in an integer using Java 8 Stream/Lambda? Basically the logic should be: keep the integer dividing by 10 as long as the remainder is 0 (the quotient will be supplied to the next division) and count the occurrence(s). e.g. 12300 % 10 == 0 true 1230 % 10 == 0 true 123 % 10 == 0 false Answer: 2 Note: I prefer not to involve String here :-) 回答1: If this is a purely hypothetical question, here is a purely hypothetical answer of how you can do it: static

Java8: Using Function::identity in Collectors.toMap(..) creates an argument mismatch error

佐手、 提交于 2019-12-23 22:36:34
问题 Given this simple Person POJO: public class Person { private String id; private String name; public Person(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } } I would like to collect a Map<String, Person> where the key is the id of the Person. I have tried to implement it like this: import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import

Java8 Stream compiler message — local variable must be final or effectively final

末鹿安然 提交于 2019-12-23 15:36:34
问题 I have a little problem. When I write this for loop, the variable i in f.getAnswerScore().get(i).... is underlined with error message : - Local variable i defined in an enclosing scope must be final or effectively final. Does this have something to do with streams? Maybe streams can't be used in loops? for (int i = 0; i < 10; i++) { correct = active.stream() .filter(f -> f.getAnswerScore().get(i).getStatus().equals(AnswerStatus.ANSWERED_CORRECT)) .count(); } 回答1: Like anonymous inner classes,

Read X lines at a time from a text file using Java Streams?

做~自己de王妃 提交于 2019-12-23 13:22:14
问题 I have a "plain old text file" where lines end with a new line character. For arbitrary reasons I need to read and parse this text file 4 (X for generality) lines at a time. I'd like to use Java streams for this task and I know I can turn the file into a stream like so: try (Stream<String> stream = Files.lines(Paths.get("file.txt""))) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } But how can I use Java's Stream API to "bunch" the file into groups of 4

Java 8 forEach over multiple IntStreams

别来无恙 提交于 2019-12-23 10:51:21
问题 I have the following code: IntStream.range(0, width).forEach(x1 -> { IntStream.range(0, height).forEach(y1 -> { IntStream.rangeClosed(x1-1, x1+1).forEach(x2 -> { IntStream.rangeClosed(y1-1, y1+1).forEach(y2 -> { if ((x1 != x2 || y1 != y2) && getNode(x2, y2) != null){ getNode(x1, y1).registerObserverAtNeighbor(getNode(x2, y2)); } }); }); }); }); Is there a way to write the above using fewer nested statements? It's basically "for each node from (0,0) to (width,height) register observer at nodes