java-8

Split stream into substreams with N elements

早过忘川 提交于 2020-06-16 21:15:45
问题 Can we somehow split stream into substreams with no more than N elements in Java? For example Stream<Integer> s = Stream.of(1,2,3,4,5); Stream<Stream<Integer>> separated = split(s, 2); // after that separated should contain stream(1,2), stream(3,4), stream(5) splitting by two streams solution is correct only for 2 streams, the same for N streams will be very ugly and write-only. 回答1: You can't split a Stream into 2 or more Streas s easily and directly. The only way the procedural one

Java8 - How does explicit type matches one variant - not other type?

好久不见. 提交于 2020-06-16 03:02:10
问题 I have a simple snippet as below. I referred this List<Document> list = new LinkedList<Document>(); FindIterable<Document> itr = collection.find(findQuery) .forEach((Document doc) -> list.add(doc)); return list; It compiles without any issues. I guess that we are telling compiler that doc is of type Document . Why is it needed? But If I do the below, it throws ambiguous error. I referred this But couldn't relate and understand exactly. collection.find(findQuery).forEach(list::add); Could

Using values from previously chained thenCompose lambdas in Java 8

核能气质少年 提交于 2020-06-14 00:26:32
问题 The Java 8 coding style preferred by my colleagues is chaining asynchronous calls all the way through, e.g. CompletionStage<E> someMethod() { return doSomething().thenCompose(a -> { // ... return b; }).thenCompose(b -> { // ... return c; }).thenCompose(c -> { // ... return d; }).thenApply(d -> { // ... return e; }); } I have something like the above, but with an added challenge: I need to recall values retrieved within some of the lambdas, in later lambdas. For example, CompletionStage<E>

How Java8's Collection.parallelStream works?

做~自己de王妃 提交于 2020-06-13 06:53:30
问题 Collection class comes with a new method " parallelStream " in Java SDK 8. It is obvious that this new method provides a mechanism to consume collections in parallel. But, I wonder about how Java achieve this parallelism. What is the underlying mechanism? Is it simply a multithreaded execution? Or does fork/join framework (coming with Java SDK 7) step in? If the answer is neither then, how does it work and what are the advantages of it over the other two mechanisms? 回答1: Looking at the stream

How Java8's Collection.parallelStream works?

谁说胖子不能爱 提交于 2020-06-13 06:53:09
问题 Collection class comes with a new method " parallelStream " in Java SDK 8. It is obvious that this new method provides a mechanism to consume collections in parallel. But, I wonder about how Java achieve this parallelism. What is the underlying mechanism? Is it simply a multithreaded execution? Or does fork/join framework (coming with Java SDK 7) step in? If the answer is neither then, how does it work and what are the advantages of it over the other two mechanisms? 回答1: Looking at the stream

Parameter of Optional.orElse gets called even when Optional contains value [duplicate]

那年仲夏 提交于 2020-06-12 15:24:31
问题 This question already has answers here : When I need to use Optional.orElseGet() over Optional.orElse() (3 answers) Closed 2 years ago . The below code prints: should Not have called hello world Why is orElse getting executed even when we have an Optional that contains a value? public static void main(String[] args){ String o = Optional.ofNullable("world").map(str -> "hello" + str).orElse(dontCallMe()); System.out.println(o); } private static String dontCallMe() { System.out.println("should

Using Java Predicate and Lambda

爷,独闯天下 提交于 2020-06-11 21:53:47
问题 Why does the below code return Predicate<String> and not boolean ? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean . So in my lambda should my nonEmptyStringPredicate not be of type boolean ? Obviously, it's not, I'm just trying to understand why it's not. Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); 回答1: A Predicate gets in this case a String as parameter and returns a boolean .

Using Java Predicate and Lambda

笑着哭i 提交于 2020-06-11 21:51:07
问题 Why does the below code return Predicate<String> and not boolean ? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean . So in my lambda should my nonEmptyStringPredicate not be of type boolean ? Obviously, it's not, I'm just trying to understand why it's not. Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); 回答1: A Predicate gets in this case a String as parameter and returns a boolean .

How can we pass a variable field /method name in Comparator.comparing

∥☆過路亽.° 提交于 2020-06-11 08:11:48
问题 I have a Report {String name, Date date, int score } class. I want to be able to sort a list of reports for any member variable using the new java 8 syntax So java 8 provides this new list.sort(Comparator.comparing(report -> report.name)) to sort the list on name. Lets say instead of name I want to provide a variable field name to this method eg. something like list.sort(Comparator.comparing(report -> report.anyField)) where anyField can be name or date or score. How do I achieve this

How can we pass a variable field /method name in Comparator.comparing

柔情痞子 提交于 2020-06-11 08:11:08
问题 I have a Report {String name, Date date, int score } class. I want to be able to sort a list of reports for any member variable using the new java 8 syntax So java 8 provides this new list.sort(Comparator.comparing(report -> report.name)) to sort the list on name. Lets say instead of name I want to provide a variable field name to this method eg. something like list.sort(Comparator.comparing(report -> report.anyField)) where anyField can be name or date or score. How do I achieve this