java-stream

Java8 toMap exception message confusing [duplicate]

孤者浪人 提交于 2019-12-13 11:19:28
问题 This question already has answers here : Why does Collectors.toMap report value instead of key on Duplicate Key error? (4 answers) Java 8 toMap IllegalStateException Duplicate Key (2 answers) Ignore duplicates when producing map using streams (6 answers) Alternative for throwingMerger in Java 8 (1 answer) Collectors.toMap with same keys(print same key) (2 answers) Closed last year . Got this exception: java.lang.IllegalStateException: Duplicate key 50 I looked over every map that is using

Java 8 Streams how to avoid filtering with map or set? [duplicate]

孤者浪人 提交于 2019-12-13 04:27:02
问题 This question already has answers here : Java 8, Streams to find the duplicate elements (14 answers) Closed 5 months ago . I keep running into solutions where I want/think i need to save state via either a Map or a Set. e.g. create a method that returns duplicates found in the input // non streams solution public int[] getDuplicates(int[] input){ Set<Integer> allSet = new HashSet<Integer>(); Set<Integer> duplicates = new HashSet<Integer>(); int[] dups = new int[input.length]; int j = 0; for

Files.walk(), calculate total size

余生颓废 提交于 2019-12-12 21:22:20
问题 I'm trying to calculate the size of the files on my disc. In java-7 this could be done using Files.walkFileTree as shown in my answer here. However if i wanted to do this using java-8 streams it will work for some folders, but not for all. public static void main(String[] args) throws IOException { long size = Files.walk(Paths.get("c:/")).mapToLong(MyMain::count).sum(); System.out.println("size=" + size); } static long count(Path path) { try { return Files.size(path); } catch (IOException |

What's the better way to add elements from a Stream to an existing List?

两盒软妹~` 提交于 2019-12-12 19:20:51
问题 I have to write some code that adds the content of a Java 8 Stream to a List multiple times, and I'm having trouble figuring out what's the best way to do it. Based on what I read on SO (mainly this question: How to add elements of a Java8 stream into an existing List) and elsewhere, I've narrowed it down to the following options: import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class Accumulator<S, T> { private

How to perform an outer join on two or more Streams

孤街浪徒 提交于 2019-12-12 17:21:02
问题 In my application I use several Streams that provide Elements of the form ( ID, value ). An Element is defined by the following class: static final class Element<T> implements Comparable<Element<T>> { final long id; final T value; Element(int id, T value) { this.id = id; this.value = value; } @Override public int compareTo(Element o) { return Long.compare(id, o.id); } } My goal is to join two or more Streams by the Element's IDs (in each stream, the IDs are sorted and strictly monotonic), e.g

Java 8 Stream.skip with Predicate [duplicate]

核能气质少年 提交于 2019-12-12 17:04:01
问题 This question already has answers here : Equivalent of Scala dropWhile (2 answers) Closed 3 years ago . Is there any way to do something similar to Stream.skip(long) but using Predicate instead of an exact number? I need to skip elements until I reach one with a given ID and then I would need to continue applying filters, limit etc. Any suggestion? 回答1: EDIT: A more concise version is https://stackoverflow.com/a/52270820/57695 I suspect you need to write a stateful predicate or use a wrapper

pick 2 elements from stream at a time [duplicate]

ε祈祈猫儿з 提交于 2019-12-12 11:36:45
问题 This question already has answers here : Split list into multiple lists with fixed number of elements in java 8 (6 answers) Closed 4 years ago . Suppose we have a collection (or stream?) {"1","2","3","4"...} now we want to take two elements and compose a pair like this {{"1","2"}, {"2","3"},{"3","4"}..} We know how to do that the ugly way (for loop). I wonder how do we do this with java 8 streams? Because approaches in java streams and reactive extensions are pretty much same (both are monads

Java 9 takeWhile and dropWhile to read and skip certain lines

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 10:43:38
问题 I have a text file that contains multiple reports in it. Each report starts with a literal "REPORT ID" and have a specific value i.e ABCD. For simple case, I want to extract data of only those reports which have their value ABCD for example. And for complexity, I want to extract data of only those reports which have TAG1 value (2nd line)as 1000375351 and report value is same as ABCD. I have done it using traditional way. My decideAndExtract(String line) function have the required logic. But

Using Streams with primitives data types and corresponding wrappers

狂风中的少年 提交于 2019-12-12 10:32:46
问题 While playing around with Java8's Streams-API, I stumbled over the following: To convert an array of primitive wrapper classe objects into a Stream I just have to call Stream.of(array) . But to convert an array of primitive data types, I have to call .of(array) from the corresponding wrapper (class) stream class (<-- that sounds silly). An example: final Integer[] integers = {1, 2, 3}; final int[] ints = {1, 2, 3}; Stream.of(integers).forEach(System.out::println); //That works just fine

stream reduction incompatible types

蓝咒 提交于 2019-12-12 09:48:16
问题 I'm trying to create a finder which takes several predicates and reduces them: public static <T extends BusinessInterface> Collection<T> findOr( Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) { Predicate<? super T> lReducedPredicate = Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false); return find(pContext, pClass, lReducedPredicate); } Unfortunately I get following compiler error: Predicate lReducedPredicate = Arrays.asList(pPredicates)