collectors

Java lang IllegalAccess on collecting Guava Immutable Table via HashBasedTable accumulator

天大地大妈咪最大 提交于 2019-12-17 14:28:26
问题 Error while executing below code, Caused by: java.lang.IllegalAccessError: tried to access class com.google.common.collect.AbstractTable from class ImmutableTable.copyOf(listItemsToProcess.parallelStream() .map(item -> ProcessorInstanceProvider.getInstance() .buildImmutableTable(item)) .collect(() -> HashBasedTable.create(), HashBasedTable::putAll, HashBasedTable<Integer, String, Boolean>::putAll) ); Error in coming on - HashBasedTable::putAll Using Oracle's 1.8 jre 回答1: This is a compiler

What's the purpose of partitioningBy

久未见 提交于 2019-12-17 10:56:20
问题 For example, if I intend to partition some elements, I could do something like: Stream.of("I", "Love", "Stack Overflow") .collect(Collectors.partitioningBy(s -> s.length() > 3)) .forEach((k, v) -> System.out.println(k + " => " + v)); which outputs: false => [I] true => [Love, Stack Overflow] But for me partioningBy is only a subcase of groupingBy . Although the former accepts a Predicate as parameter while the latter a Function , I just see a partition as a normal grouping function. So the

Stream.skip behavior with unordered terminal operation

血红的双手。 提交于 2019-12-17 07:21:33
问题 I've already read this and this questions, but still doubt whether the observed behavior of Stream.skip was intended by JDK authors. Let's have simple input of numbers 1..20: List<Integer> input = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList()); Now let's create a parallel stream, combine the unordered() with skip() in different ways and collect the result: System.out.println("skip-skip-unordered-toList: " + input.parallelStream().filter(x -> x > 0) .skip(1) .skip(1)

Performance difference between Stream.map and Collectors.mapping [duplicate]

依然范特西╮ 提交于 2019-12-11 14:26:22
问题 This question already has answers here : What's the difference between Stream.map(…) and Collectors.mapping(…)? (2 answers) Why Stream operations is duplicated with Collectors? (2 answers) Collectors.summingInt() vs mapToInt().sum() (1 answer) Closed last month . Last time I was discovering nooks of the functional programming of Java 8 and above and I found out a static method mapping in Collectors class. We have a class Employee like: @AllArgsConstructor @Builder @Getter public class

Java Streams: Grouping, Summing AND Counting

我的未来我决定 提交于 2019-12-11 10:38:24
问题 I'm new to streams but very intrigued with the possibilities. I'm trying to write a stream that does grouping, counting and summing at the same time. The data involved is actually quite simple but writing the streaming statement I need is proving challenging and I'm not seeing anything really helpful in Google searches. First, let me describe my data, then I'll show you how I've solved two-thirds of the problem. Perhaps you can tell me how to fit in the missing piece. The data is ticket sales

How to auto increment the key of a hashmap using collectors and stream in java 8

旧巷老猫 提交于 2019-12-11 06:35:36
问题 I am new to Java 8: Streams and Collectors classes. I am reading a file whose content need to be saved in a LinkedHashMap<Integer, String> where its <keys> are the line numbers of the files and its <values> are the content at each line, respectably. Here, I want to use the Stream concept but I am not able to use Collectors.toMap to auto-increment the <keys> , which need to be saved in the LinnkedHashMap object. Instead of that I am getting exceptions. Following is the code which i am trying:

Runtime complexity of Collectors#toList

牧云@^-^@ 提交于 2019-12-11 00:03:13
问题 In the Java library source code, the Collectors#toList method is defined like this: public static <T> Collector<T, ?, List<T>> toList() { return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID); } We see BinaryOperator as third parameter of CollectorImpl 's contructor, which merges two sub-results in linear time. Does it mean, that in case of frequent usage of this function by Stream#collect method, we can gain

Java 8 Collection stream Collectors.toMap

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:25:36
问题 Why this code is not compile for me? I try to convert List to Map using stream and toMap option List<CountryToPaymentMethodsDisplayRules> paymentMethodDisplayRulesByCountryList = gatway.getCountryPaymentMethodsDisplayRulesByCountry(); Map<PaymentMethod, CountryToPaymentMethodsDisplayRules> countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList .stream() .collect(Collectors.toMap(type -> type.getCountryToPaymentMethodsDisplayRules().getPaymentMethod(), type -> type));

Filter on map of map

陌路散爱 提交于 2019-12-10 10:37:49
问题 I have below map of map and want to filter it based on a value. The result should be assigned back to same map. Please let know what is the best approach for this. Map<String, Map<String, Employee>> employeeMap; < dep1, <"empid11", employee11> <"empid12",employee12> dep2, <"empid21", employee21> <"empid22",employee22> > Filter: employee.getState="MI" I tried like below but i was not able to access the employee object currentMap = currentMap.entrySet().stream() **.filter(p->p.getValue()

Java 8 stream.collect( … groupingBy ( … mapping( … reducing ))) reducing BinaryOperator-usage

两盒软妹~` 提交于 2019-12-09 16:23:54
问题 I played around with a solution using groupingBy , mapping and reducing to the following question: Elegantly create map with object fields as key/value from object stream in Java 8. Summarized the goal was to get a map with age as key and the hobbies of a person as a Set . One of the solutions I came up with (not nice, but that's not the point) had a strange behaviour. With the following list as input: List<Person> personList = Arrays.asList( new Person(/* name */ "A", /* age */ 23, /*