java-stream

Why is Streams.allMatch(in Java8) trying to evaluate all the expressions even if the value can be determined midway?

折月煮酒 提交于 2020-04-06 18:43:12
问题 Consider this snippet - String a = "hello" , b = null, c = "guru"; boolean value = Stream .of(a, b, b.substring(2),c) .allMatch(x -> x != null); System.out.println(value); This results in NPE. It seems to be doing b.substring(2) and since b is null , NPE is thrown. Why is this condition evaluated? The second expression b is null and hence evaluates to false . So, allMatch will be false regardless of the truth values of the subsequent operations. In that case, why is it trying to evaluate b

Why is Streams.allMatch(in Java8) trying to evaluate all the expressions even if the value can be determined midway?

孤街浪徒 提交于 2020-04-06 18:42:58
问题 Consider this snippet - String a = "hello" , b = null, c = "guru"; boolean value = Stream .of(a, b, b.substring(2),c) .allMatch(x -> x != null); System.out.println(value); This results in NPE. It seems to be doing b.substring(2) and since b is null , NPE is thrown. Why is this condition evaluated? The second expression b is null and hence evaluates to false . So, allMatch will be false regardless of the truth values of the subsequent operations. In that case, why is it trying to evaluate b

Retrieving list of employees with lowest salary using stream [duplicate]

余生颓废 提交于 2020-04-05 16:04:32
问题 This question already has answers here : How to force max to return ALL maximum values in a Java Stream? (5 answers) Closed 7 months ago . I'm trying to retrieve a list of those with lowest salary from a list of employees. So far I've managed to find the employee with lowest salary, but I want to retrieve multiple if several employees have the same salary. I think the solution is supposed to be in one line. So I can't create a variable with the lowest salary and just check each one for

Java 8 stream max() function argument type Comparator vs Comparable

半城伤御伤魂 提交于 2020-04-05 15:49:20
问题 I wrote some simple code like below. This class works fine without any errors. public class Test { public static void main(String[] args) { List<Integer> intList = IntStream.of(1,2,3,4,5,6,7,8,9,10).boxed().collect(Collectors.toList()); int value = intList.stream().max(Integer::compareTo).get(); //int value = intList.stream().max(<Comparator<? super T> comparator type should pass here>).get(); System.out.println("value :"+value); } } As the code comment shows the max() method should pass an

Problems using interactive debuggers with Java 8 streams

筅森魡賤 提交于 2020-03-18 04:07:28
问题 I love Java 8 streams. They are intuitive, powerful and elegant. But they do have one major drawback IMO: they make debugging much harder (unless you can solve your problem by just debugging lambda expressions, which is answered here). Consider the following two equivalent fragments: int smallElementBitCount = intList.stream() .filter(n -> n < 50) .mapToInt(Integer::bitCount) .sum(); and int smallElementBitCount = 0; for (int n: intList) { if (n < 50) { smallElementBitCount += Integer

Java parallel stream: how to wait for threads for a parallel stream to finish?

时光怂恿深爱的人放手 提交于 2020-03-17 10:49:02
问题 So I have a list from which I obtain a parallel stream to fill out a map, as follows: Map<Integer, TreeNode> map = new HashMap<>(); List<NodeData> list = some_filled_list; //Putting data from the list into the map list.parallelStream().forEach(d -> { TreeNode node = new TreeNode(d); map.put(node.getId(), node); }); //print out map map.entrySet().stream().forEach(entry -> { System.out.println("Processing node with ID = " + entry.getValue().getId()); }); The problem with this code is that the

Java parallel stream: how to wait for threads for a parallel stream to finish?

大憨熊 提交于 2020-03-17 10:48:07
问题 So I have a list from which I obtain a parallel stream to fill out a map, as follows: Map<Integer, TreeNode> map = new HashMap<>(); List<NodeData> list = some_filled_list; //Putting data from the list into the map list.parallelStream().forEach(d -> { TreeNode node = new TreeNode(d); map.put(node.getId(), node); }); //print out map map.entrySet().stream().forEach(entry -> { System.out.println("Processing node with ID = " + entry.getValue().getId()); }); The problem with this code is that the

Is there anything wrong with using I/O + ManagedBlocker in Java8 parallelStream()?

◇◆丶佛笑我妖孽 提交于 2020-03-17 04:31:39
问题 The default "paralellStream()" in Java 8 uses the common ForkJoinPool which may be a latency problem if the common Pool threads are exhausted when a task is submitted. However in many cases enough CPU power is available and the tasks are short enough so that this is not a problem. If we do have some long running tasks this will of course need some careful consideration, but for this question let's assume that this is not the problem. However filling the ForkJoinPool with I/O tasks that don't

Check whether list of custom objects have same value for a property in Java 8

别来无恙 提交于 2020-03-13 07:19:50
问题 I am new to Java 8. I have a list of custom objects of type A, where A is like below: class A { int id; String name; } I would like to determine if all the objects in that list have same name. I can do it by iterating over the list and capturing previous and current value of names. In that context, I found How to count number of custom objects in list which have same value for one of its attribute. But is there any better way to do the same in java 8 using stream? 回答1: One way is to get the

Custom Collector to join stream on delimiter, suffix and prefix only if stream is not empty suffix

霸气de小男生 提交于 2020-03-05 05:40:15
问题 I have a stream of strings: Stream<String> stream = ...; I want to create a string using stream.collect(Collectors.joining(',', '[', ']')) only I want to return "No Strings" if the stream does not contain any elements. I notice that String java.util.stream.Stream.collect(Collector<? super String, ?, String> collector) method takes an argument of type java.util.stream.Collector For my project I need to this functionality in many places so I would need a class the implements the Collector