java-stream

java: check if any element in array 1 is present in array 2

余生长醉 提交于 2019-12-11 19:45:46
问题 I am fairly new to java streams and wonder whether there is an easy solution using java streams to check if any element in array 1 is also present in array 2 example: array1 = ["banana","apple","cat"] array2 = ["toast","bread","pizza","banana"] --> return true array1 = ["banana","apple","cat"] array2 = ["toast","bread","pizza"] --> return false Thanks! 回答1: Just use Collections.disjoint. This method checks if any elements of the both arrays are common. Collections.disjoint(Arrays.asList

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

ParallelStream toArray of higher size Java8

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 12:58:24
问题 I am trying to create an array with higher size from a parallel stream myList.parallelStream().map((listElm) -> { return someObjectMorpher(listElm);}).toArray(size -> new SomeObject[size+4]); Is it guaranteed that the last 4 elements of the array will always be null? Which means toArray will always populate at first indices of the Array. Is there a possibility of having toArray populate an already created array SomeObjet [] arr = new SomeObject[myList.size() + 4]; arr[0] = new SomeObject(x);

sort a list of characters using lambdas in java

為{幸葍}努か 提交于 2019-12-11 12:18:40
问题 ok this is homework. I have tried a couple things but just cant get it to work. the instructions are--Write a program that inserts 30 random letters into a List. Perform the following operations and display your results: a) Sort the List in ascending order. b) Sort the List in descending order. c) Display the List in ascending order with duplicates removed. I have the simplest part done but when it comes to the sorting im not sure where to start. public class RandomCharacters { // private

Sorted stream derived from Infinite Stream fails to iterate

会有一股神秘感。 提交于 2019-12-11 11:57:26
问题 import java.util.stream.*; import java.util.*; class TestInfiniteStream { public static void main(String args[]) { IntStream infiniteStream = new Random().ints(); IntStream sortedStream = infiniteStream.sorted(); sortedStream.forEach(i -> System.out.println(i)); } } After compiling and executing this code I get the following error. Exception in thread "main" java.lang.IllegalArgumentException: Stream size exceeds max array size Does sorting a stream fail on an infinite stream? 回答1: The simple

java 8 stream API filter out empty value from map

有些话、适合烂在心里 提交于 2019-12-11 10:40:36
问题 i have a map, need to operate on each entry's value, and return the modified map. I managed to get it working, but the resulted map contains entries with empty value, and I want to remove those entries but cannot with Java 8 stream API. here is my original code: Map<String, List<Test>> filtered = Maps.newHashMap(); for (String userId : userTests.keySet()) { List<Test> tests = userTests.get(userId); List<Test> filteredTests = filterByType(tests, supportedTypes); if (!CollectionUtils.isEmpty

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

Generate infinite parallel stream

此生再无相见时 提交于 2019-12-11 08:50:54
问题 Problem Hi, I have a function where i going to return infinite stream of parallel (yes, it is much faster in that case) generated results. So obviously (or not) i used Stream<Something> stream = Stream.generate(this::myGenerator).parallel() It works, however ... it doesn't when i want to limit the result (everything is fine when the stream is sequential). I mean, it creates results when i make something like stream.peek(System.out::println).limit(2).collect(Collectors.toList()) but even when

Iterating through nested collections to find first sub-sub-sub element matching a criterion

狂风中的少年 提交于 2019-12-11 08:07:23
问题 I have an object with a List of other objects, each other object has a List etc. I need to find in the hierarchy the first (and unique) last element in the hierarchy that has a property matching some value. Seeing my present code will be clearer : @Override public Poste findByNumeroAndMillesime(String numero, Millesime millesime) { return millesime .getDivisions() .stream() .filter( division -> division .getGroupes() .stream() .filter( groupe -> groupe .getClasses() .stream() .filter( classe

Java 8 streams - passing a method to a filter

一世执手 提交于 2019-12-11 07:31:24
问题 I have a set of integers set2 and an object: public class Bucket { private Integer id; private Set<Integer> set1; ... } I want to filter Buckets using streams, but only buckets where their set1 has intersection with another set2. I tried the following code: Set<Bucket> selectedBuckets = allBuckets.stream() .filter(e -> Sets.intersection(e.getSet1(), set2).size()>1) .collect(Collectors.toSet()); But this would return all elements of allBuckets, instead of only the elements whose sets contains