collectors

Is there a Collector that collects to an order-preserving Set?

不问归期 提交于 2019-12-28 08:39:13
问题 Collectors.toSet() does not preserve order. I could use Lists instead, but I want to indicate that the resulting collection does not allow element duplication, which is exactly what Set interface is for. 回答1: You can use toCollection and provide the concrete instance of the set you want. For example if you want to keep insertion order: Set<MyClass> set = myStream.collect(Collectors.toCollection(LinkedHashSet::new)); For example: public class Test { public static final void main(String[] args)

Java 8 Collectors.toMap SortedMap

佐手、 提交于 2019-12-28 04:59:42
问题 I'm using Java 8 lambdas and want to use Collectors toMap to return a SortedMap . The best I can come up with is to call the following Collectors toMap method with a dummy mergeFunction and mapSupplier equal to TreeMap::new . public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) { BiConsumer<M, T> accumulator = (map, element) ->

Java 8: Collector groupby with list nested class

半世苍凉 提交于 2019-12-25 16:55:13
问题 I am trying to get following Map via Java 8 Class One { String one; List <Two> two; } Class Two { BigDecimal bd; } How do I collect a Map which contains grouping by One.one i.e., first parameter of map. For second parameter of map sum of Two.bd. 回答1: You can use this: List<One> list = ...; Map<String, BigDecimal> result1 = list.stream() .collect(Collectors.groupingBy(One::getOne, // key is One.one Collectors.mapping(one -> one.getTwo().stream() // get stream of One.two .map(Two::getBd) // map

Java8: Using Function::identity in Collectors.toMap(..) creates an argument mismatch error

佐手、 提交于 2019-12-23 22:36:34
问题 Given this simple Person POJO: public class Person { private String id; private String name; public Person(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } } I would like to collect a Map<String, Person> where the key is the id of the Person. I have tried to implement it like this: import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import

How to use method reference in Java 8 for Map merge?

妖精的绣舞 提交于 2019-12-23 22:19:29
问题 I have following 2 forms of calling a collect operation, both return same result, but I still cannot depend fully on method references and need a lambda. <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) For this consider the following stream consisting on 100 random numbers List<Double> dataList = new Random().doubles().limit(100).boxed() .collect(Collectors.toList()); 1) Following example uses pure lambdas Map<Boolean, Integer> partition =

Java 8 streams: conditional Collector

杀马特。学长 韩版系。学妹 提交于 2019-12-22 09:33:01
问题 I want to use Java 8 streams to convert a List of String values to a single String. A List of values like "A", "B" should return a String like "Values: 'A', 'B' added". This works fine, however I want to change the Pre- and Postfix depending on the amount of values. For example, if I have a List of only "A" I want the resulting String to be "Value 'A' added". import java.util.stream.Collectors; import java.util.ArrayList; import java.util.List; public class HelloWorld { public static void

Java Streams: get values grouped by inner map key

不问归期 提交于 2019-12-22 03:23:16
问题 I have Map<A, Map<B, C>> and I want to get Map<B, List<C>> from it using Java Streams. I try to do it as follows: public <A, B, C> Map<B, List<C>> groupsByInnerKey(Map<A, Map<B, C>> input) { return input.values() .stream() .flatMap(it -> it.entrySet().stream()) .collect(Collectors.groupingBy(Map.Entry::getKey)); } What I expect: flatMap gives a Stream of Map.Entry<B, C> collect(Collectors.groupingBy(...)) takes function which is applied to Map.Entry<B, C> and returns B , thus it collects

Group, Collectors, Map (Int to String), Map (Map to Object)

时光总嘲笑我的痴心妄想 提交于 2019-12-21 22:48:43
问题 This is a continuation of my previous question at Group, Sum byType then get diff using Java streams. As suggested, I should post as a separate thread instead of updating the original one. So with my previous set of question, I have achieved that, and now, with the continuation. Background: I have the following dataset Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1,

What does the Java 8 Collector UNORDERED characteristic mean?

徘徊边缘 提交于 2019-12-21 04:28:08
问题 In official documentation you can read that: UNORDERED Indicates that the collection operation does not commit to preserving the encounter order of input elements. This is not too helpful without any examples. My question is, what exactly does UNORDERED characteristic mean? Should I use it with reducing collectors like min or sum or is it only applicable to collection collectors? In OpenJDK looks like reducing operations (min, sum, avg) have empty characteristics. I expected to find there at

issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

微笑、不失礼 提交于 2019-12-20 18:43:11
问题 i was having working code with earlier version of java 8 which i was using to get unique values from list but since i upgraded to JDK 66 its giving me an error Type mismatch: cannot convert from List<Object> to List<String> List<String> instList = new ArrayList<String>(); while (res.next()) { instList.add(res.getString("INST").toString()); } List<String> instListF = instList.stream().distinct().collect(Collectors.toList()); Where res is resultset i am getting from database, not sure what is