java-stream

stream map with Collectors.toMap vs Accumulator

隐身守侯 提交于 2020-05-14 14:18:51
问题 I have a a set of enums Set<Task> tasks; and process this to an EnumMap: Version 1 (using supplier, accumulator, combiner): return task.stream() .collect(() -> new EnumMap<>(Task.class), (map, item) -> map.put(item, compute(item)), Map::putAll); Version 2 (using Collectors.toMap): return tasks.stream().collect( Collectors.toMap( Function.identity(), t -> compute(t), null, () -> new EnumMap<>(Task.class))); I like version 1 more because it is shorter. However, I must select the fastest version

Need to cast the list again after already filtering and casting objects with Java 8 Stream API

倖福魔咒の 提交于 2020-05-13 13:57:21
问题 I have a list of objects, and I want to filter out those objects of a specific type, cast and collect them into a new list. That's why I use the stream API suggested in the following questions: Java 8 Stream API : Filter on instance, and cast Is it possible to cast a Stream in Java 8? However, Intellij tells me that I have to cast the collected list again after filtering&casting&collecting, I want to figure out why? And, if I accept the suggestion, the error goes away, but is that the right

how to use java stream to group fields and create a summary

不想你离开。 提交于 2020-05-13 11:48:07
问题 public class Call { private String status; private String callName; } I have a list of calls and i have to create a summary, like this: public class CallSummary { private String callName; private List<ItemSummary> items; } public class itemSummary { private String status; private Integer percentage; } My goal is show a percentage of calls with some status like : INBOUND_CALL : { FAILED = 30% SUCCESS = 70% } how can i do it using java 8 stream and Collectors ? 回答1: The idea behind the grouping

grouping and sum with nested lists

孤者浪人 提交于 2020-05-13 04:55:43
问题 I have nested lists and I'm trying to group and sum to get the desired result using java streams and collectors . With this I'm not able to loop over multiple SubAccounts . Either I have to use for loop or some other logic. I want to achieve using streams api. Is there any possibility for that Map<Long, BigDecimal> assetQuanMap = subAccounts.getAssets.parallelStream().collect(Collectors.groupingBy(Asset::getAssetId, Collectors.reducing(BigDecimal.ZERO, Asset::getQuantity, BigDecimal::add)));

grouping and sum with nested lists

情到浓时终转凉″ 提交于 2020-05-13 04:55:11
问题 I have nested lists and I'm trying to group and sum to get the desired result using java streams and collectors . With this I'm not able to loop over multiple SubAccounts . Either I have to use for loop or some other logic. I want to achieve using streams api. Is there any possibility for that Map<Long, BigDecimal> assetQuanMap = subAccounts.getAssets.parallelStream().collect(Collectors.groupingBy(Asset::getAssetId, Collectors.reducing(BigDecimal.ZERO, Asset::getQuantity, BigDecimal::add)));

How to collect the results of a Stream in a primitive array?

白昼怎懂夜的黑 提交于 2020-05-11 04:47:41
问题 I'm trying to convert 2D list to a 2D int array. However, it seems I can only collect objects, not primitives. When I do: data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new); I get the compile-time error Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) . However, if I change int[] to Integer[] , it compiles. How can I get it to just use int ? 回答1: Use mapToInt method to produce a stream of primitive integers: int[][] res = data.stream().map(l

How to map and collect primitive return type using Java 8 Stream

亡梦爱人 提交于 2020-05-10 20:08:20
问题 I am new in Java 8 streams and I am wondering if there is way to perform forEach/map call on method returning a byte and accepting an int as parameter. Example: public class Test { private byte[] byteArray; // example of byte array public byte getByte(int index) { return this.byteArray[index]; } public byte[] getBytes(int... indexes) { return Stream.of(indexes) .map(this::getByte) // should return byte .collect(byte[]::new); // should return byte[] } } As you may guess, the getBytes method

How to map and collect primitive return type using Java 8 Stream

六月ゝ 毕业季﹏ 提交于 2020-05-10 20:07:11
问题 I am new in Java 8 streams and I am wondering if there is way to perform forEach/map call on method returning a byte and accepting an int as parameter. Example: public class Test { private byte[] byteArray; // example of byte array public byte getByte(int index) { return this.byteArray[index]; } public byte[] getBytes(int... indexes) { return Stream.of(indexes) .map(this::getByte) // should return byte .collect(byte[]::new); // should return byte[] } } As you may guess, the getBytes method

Java8 group a list of lists to map

无人久伴 提交于 2020-05-07 19:15:28
问题 I have a Model and a Property class with the following signatures: public class Property { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Model { private List<Property> properties = new ArrayList<>(); public List<Property> getProperties() { return properties; } } I want a Map<String, Set<Model>> from a List<Model> where the key would be the name from the Property class. How can I can I use java8 streams to

Java8 group a list of lists to map

痞子三分冷 提交于 2020-05-07 19:14:18
问题 I have a Model and a Property class with the following signatures: public class Property { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Model { private List<Property> properties = new ArrayList<>(); public List<Property> getProperties() { return properties; } } I want a Map<String, Set<Model>> from a List<Model> where the key would be the name from the Property class. How can I can I use java8 streams to