collectors

Transform and filter a Java Map with streams

血红的双手。 提交于 2019-12-20 08:56:35
问题 I have a Java Map that I'd like to transform and filter. As a trivial example, suppose I want to convert all values to Integers then remove the odd entries. Map<String, String> input = new HashMap<>(); input.put("a", "1234"); input.put("b", "2345"); input.put("c", "3456"); input.put("d", "4567"); Map<String, Integer> output = input.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> Integer.parseInt(e.getValue()) )) .entrySet().stream() .filter(e -> e.getValue() % 2 == 0)

Java 8 frequency Object in array [duplicate]

与世无争的帅哥 提交于 2019-12-19 09:53:10
问题 This question already has answers here : Word frequency count Java 8 (8 answers) Closed 4 years ago . I have an Object[] array I need to create map Map<Obejct, Integer> , where Integer value contains frequency of key Object in array. How can i do it in java 8 style, using Collectors ? 回答1: You can do (I hope I don't have any typos) : Map<Object,Long> map = Stream.of(array) .collect(Collectors.groupingBy(o -> o, Collectors.counting())); This should group the elements of the array by equality

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

Understanding downstream reduction implementation

断了今生、忘了曾经 提交于 2019-12-19 03:35:11
问题 I'm trying to understand the implementation of downstream reduction in JDK. Here is it: public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream) { Supplier<A> downstreamSupplier = downstream.supplier(); BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); BiConsumer<Map<K, A>, T> accumulator = (m, t) -> { K key = Objects.requireNonNull

Counting elements of a Stream

让人想犯罪 __ 提交于 2019-12-18 21:22:28
问题 I want to count the different elements of a stream and am wondering why Stream<String> stream = Stream.of("a", "b", "a", "c", "c", "a", "a", "d"); Map<String, Integer> counter1 = stream.collect(Collectors.toMap(s -> s, 1, Integer::sum)); doesn't work. Eclipse tells me The method toMap(Function, Function, BinaryOperator) in the type Collectors is not applicable for the arguments (( s) -> {}, int, Integer::sum) By the way, I know about that solution: Map<String, Long> counter2 = stream.collect

Collectors.groupingBy doesn't accept null keys

拥有回忆 提交于 2019-12-18 18:35:31
问题 In Java 8, this works: Stream<Class> stream = Stream.of(ArrayList.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); But this doesn't: Stream<Class> stream = Stream.of(List.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); Maps allows a null key, and List.class.getSuperclass() returns null. But Collectors.groupingBy emits a NPE, at Collectors.java, line 907: K key =

how to use java 8 stream and lambda to flatMap a groupingBy result

血红的双手。 提交于 2019-12-18 04:21:58
问题 I have a object with contains a list of other objects and I want to return a flatmap of the contained objects mapped by some property of the container. Any one if is it possible using stream and lambdas only? public class Selling{ String clientName; double total; List<Product> products; } public class Product{ String name; String value; } Lets supose a list of operations: List<Selling> operations = new ArrayList<>(); operations.stream() .filter(s -> s.getTotal > 10) .collect(groupingBy

Confused by Java8 Collectors.toMap

假装没事ソ 提交于 2019-12-18 03:01:42
问题 I have a collection that looks like below, and I want to filter out the everything except the dates that aren't the end of the months. 2010-01-01=2100.00, 2010-01-31=2108.74, 2010-02-01=2208.74, 2010-02-28=2217.92, 2010-03-01=2317.92, 2010-03-31=2327.57, 2010-04-01=2427.57, 2010-04-30=2437.67, 2010-05-01=2537.67, 2010-05-31=2548.22, 2010-06-01=2648.22, 2010-06-30=2659.24, 2010-07-01=2759.24, 2010-07-31=2770.72, 2010-08-01=2870.72, 2010-08-31=2882.66, 2010-09-01=2982.66, 2010-09-30=2995.07,

Alternative for throwingMerger in Java 8

风流意气都作罢 提交于 2019-12-17 16:52:50
问题 I'm implementing own collector that uses merge function . Unfortunately, for some of my cases, I can't reuse the following JDK merger function that thrown IllegalStateException . java.util.stream.Collectors#throwingMerger It happens due to the fact that it has private access modifier and access from other(not inner) classes is restricted. However, javadoc says the following: This can be used to enforce the assumption that the elements being collected are distinct But, as I see, java doc is

How to get the key in Collectors.toMap merge function?

对着背影说爱祢 提交于 2019-12-17 16:45:15
问题 When a duplicate key entry is found during Collectors.toMap() , the merge function (o1, o2) is called. Question: how can I get the key that caused the duplication? String keyvalp = "test=one\ntest2=two\ntest2=three"; Pattern.compile("\n") .splitAsStream(keyval) .map(entry -> entry.split("=")) .collect(Collectors.toMap( split -> split[0], split -> split[1], (o1, o2) -> { //TODO how to access the key that caused the duplicate? o1 and o2 are the values only //split[0]; //which is the key, cannot