java-stream

How to sum up the individual fields of the object list and return the results as a single object

时光毁灭记忆、已成空白 提交于 2019-12-19 18:31:55
问题 I have a method which is calculating nutrients for a list of object which we are receiving from API request call. The method looks like: public Nutrients nutrientsCalculator(DailyMeals dailyMeals) { String foodNamesForRequest = prepareFoodNamesForRequest(dailyMeals); HttpEntity<NutrientsBodyForRequest> requestBody = prepareRequestForAPICall(foodNamesForRequest); ResponseEntity<List<FoodNutritional>> response = //create request here if (nonNull(response.getBody())) { double totalFat = response

what does java8 stream map do here?

我的梦境 提交于 2019-12-19 17:40:58
问题 I was confused about the difference between map() and forEach() method in java8 stream. For instance, List<String> strings = Lists.newArrayList("1", "2"); Map<String, String> map = Maps.newHashMap(); strings.stream().map(s->map.put(s, s)); System.out.println(map); I got empty output here, but if I change map to forEach() just like List<String> strings = Lists.newArrayList("1", "2"); Map<String, String> map = Maps.newHashMap(); strings.stream().forEach(s->map.put(s, s)); System.out.println(map

Java streams to split and collect string to a Map [duplicate]

穿精又带淫゛_ 提交于 2019-12-19 13:35:54
问题 This question already has answers here : Java 8 Split String and Create Map inside Map (2 answers) List of comma separated String to Map of list using java 8 (3 answers) Converting a text file to Map<String, List<String>> using lambda (3 answers) Splitting string with pipe character (“|”) [duplicate] (5 answers) Closed 2 days ago . I have a string like this String input = "abc|label1 cde|label2 xyz|label1 mno|label3 pqr|label2"; I want to create a Map which looks like (after filtering out

Using two streams in Java lambda to compute covariance

人走茶凉 提交于 2019-12-19 11:49:41
问题 Let's say I have two arrays of double. I've been experimenting with Stream from Java 8. I think I've understood the main ideas but then I realised that I'm not sure how to manipulate two Streams at the same time. For example, I want to calculate the covariance of both arrays. public class foo { public static double mean(double[] xs) { return Arrays.stream(xs).average().getAsDouble(); } public static void main(String[] args) { double[] xs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; double[] ys = {1517.93,

Is it possible to use Streams.intRange function?

旧城冷巷雨未停 提交于 2019-12-19 10:52:21
问题 I wanted to use Streams.intRange(int start, int end, int step) to achieve reverse ordered stream. However it seems that java.util.Streams class is no longer available (however it is still in rt.jar in standard library). Is this method in some other class or replaced with something else? 回答1: There is indeed no such method anymore in the JDK; the next closest you could get is IntStream.range() but that will only step one by one. One solution here would be to implement your own Spliterator

Why my collector method is not processing data parallely?

China☆狼群 提交于 2019-12-19 09:29:12
问题 Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the parallel invocations of the accumulator could actually deposit their results concurrently into the same shared result container, eliminating the need for the combiner to merge distinct result containers. This potentially provides a boost to the parallel execution performance. We call this a concurrent reduction. also A Collector that

Why my collector method is not processing data parallely?

蓝咒 提交于 2019-12-19 09:29:05
问题 Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the parallel invocations of the accumulator could actually deposit their results concurrently into the same shared result container, eliminating the need for the combiner to merge distinct result containers. This potentially provides a boost to the parallel execution performance. We call this a concurrent reduction. also A Collector that

Java Stream Collectors.toList() wont compile

▼魔方 西西 提交于 2019-12-19 09:28:30
问题 Can any one explain why the below code will not compile but the second one does? Do not compile private void doNotCompile() { List<Integer> out; out = IntStream .range(1, 10) .filter(e -> e % 2 == 0) .map(e -> Integer.valueOf(2 * e)) .collect(Collectors.toList()); System.out.println(out); } Compilation errors on the collect line The method collect(Supplier, ObjIntConsumer, BiConsumer) in the type IntStream is not applicable for the arguments (Collector>) Type mismatch: cannot convert from

How to create a two dimensional array from a stream in Java 8?

人盡茶涼 提交于 2019-12-19 05:47:24
问题 I have a text file like this: ids.txt 1000 999 745 123 ... I want to read this file and load it in a two dimensional array. I expect to have an array similar to the one below: Object[][] data = new Object[][] { // { new Integer(1000) }, // { new Integer(999) }, // { new Integer(745) }, // { new Integer(123) }, // ... }; Here is the code I wrote: File idsFile = ... ; try (Stream<String> idsStream = Files.lines(idsFile.toPath(), StandardCharsets.US_ASCII)) { Object[][] ids = idsStream .filter(s

Merge two maps with Java 8

南笙酒味 提交于 2019-12-19 05:23:52
问题 I have two maps like this: map1 = new Map<String, MyObject>(); map2 = new Map<String, MyObject>(); MyObject { Integer mark1; Integer mark2; } What I want to do to is to merge the two maps into a map3 <String, MyObject> like this: If map1.place is not in map2.place , then I add the entry to map3 . same if map2.place is not in map1.place , I add the entry to map3 . if map1.place is in map2.place , then I add this entry: map1.place, (map1.mark1, map2.mark2) I have read about flatMap , but I