java-stream Questions

3

I know micro-benchmarking is hard. I'm not trying to build a poor micro-benchmark. Rather, I have run into this problem when making (what I thought to be) harmless refactoring. There is stripped do...
Branch asked 14/11, 2019 at 22:54

2

Solved

I'm trying to understand why there is a different when I change from y.addAll(x) to x.addAll(y) in code snippet below: List<Integer> result = List.of(1, 2) .parallelStream() .collect( Arr...
Kavanaugh asked 3/8 at 17:39

6

Solved

I have a class where optionally a Comparator can be specified. Since the Comparator is optional, I have to evaluate its presence and execute the same stream code, either with sorted() or without...
Loincloth asked 27/7, 2017 at 6:7

1

I want to collect List<TestClone>, but looks like .collect() returns only List<Object>. Is there a way I can get List<TestClone>? I know .toArray() is there, but want an ArrayList...
Zilber asked 15/7 at 16:18

2

Solved

I'm trying to use in place of bitmask below is the code public static Set<Amenities> fromBitFlags(int bitFlag) { return ALL_OPTS.stream().filter(a -> (a.ameityId & bitFlag) > 0)....
Ladin asked 3/2, 2016 at 13:23

8

Solved

I am a beginner in using Lambda expression feature in Java 8. Lambda expressions are pretty well useful in solving programs like Prime number check, factorial etc. However can they be utilized eff...
Dignify asked 2/6, 2015 at 12:16

3

Solved

I don't feel the difference between map() and mapToObj() methods in Java 8 Streams. In both we can create and return objects to the streams, so why these methods exist as two, not only one. Could ...
Para asked 14/12, 2017 at 7:10

3

Solved

I am writing a parser for a file in Java 8. The file is read using Files.lines and returns a sequential Stream<String>. Each line is mapped to a data object Result like this: Result parse(S...
Solo asked 6/11, 2014 at 12:37

7

Solved

Im trying to use Java 8 streams to combine lists. How can I get a "symmetric difference list" (all object that only exist in one list) from two existing lists. I know how to get an intersect list a...
Outfoot asked 26/6, 2015 at 13:30

4

Solved

Lets suppose we were given the following two arrays String[] keys = new String[] {"a", "b", "c", "aa", "d", "b"}; int[] values = new int[...
Zinc asked 26/10, 2017 at 7:30

5

Solved

Say I have two arrays of Double Double[] a = new Double[]{1.,2.,3.}; Double[] b = new Double[]{10.,20.,30.}; Using Java streams, how do I create a map (Map<Double,Double> myCombinedMap;) t...
Calamite asked 13/9, 2019 at 10:45

5

I have a code fragment that I want to make more concise yet readable using Java 8 features like lambdas/streams etc. Basically, there is a list of items and each item has a list of errors. If ther...
Gardia asked 19/1, 2019 at 11:54

19

I have one string say "Aniruddh" and I want to reverse it using lambdas and streams in Java 8. How can I do it?
Rattler asked 27/11, 2017 at 5:58

2

Solved

One of the interactions I want to test is that a class Foo is supposed to pass a Stream<Changes> to FooListener.someChangesHappened. Is there a Mockito idiom to verify that a stream contained...
Henotheism asked 7/5, 2016 at 13:17

12

Solved

Lets say I have a list of words and i want to create a method which takes the size of the new list as a parameter and returns the new list. How can i get random words from my original sourceList? ...
Unblushing asked 29/7, 2015 at 14:20

7

Solved

Is there any significant difference (in performance or best practices) between those two stream creation methods? int[] arr2 = {1,2,3,4,5,6}; Arrays.stream(arr2) .map((in)->in*2) .mapToObj((...
Duester asked 31/12, 2018 at 9:19

3

Solved

JDK 8 EA is out now, and I am just trying to get used to the lambda and the new Stream API. I've tried to sort a list with parallel stream, but the result is always wrong: import java.util.ArrayLi...
Circumference asked 22/10, 2013 at 23:14

6

Solved

I am sending result in descending order but I get output with ascending order List<myEntity> myData = new ArrayList<>(); Map<Integer,List<myEntity>> myid = new LinkedHashMa...
Gonzalez asked 5/9, 2018 at 19:20

7

Solved

I want to something which is similar to the Scala grouped function. Basically, pick 2 elements at a time and process them. Here is a reference for the same: Split list into multiple lists with fixe...
Feint asked 29/1, 2015 at 9:11

7

Solved

I am trying to learn java - stream. I am able to do simple iteration / filter / map / collection etc. When I was kind of trying to collect every 3 elements and print as shown here in this example,...
Frequentation asked 27/3, 2017 at 22:45

28

Solved

Is there a concise way to iterate over a stream whilst having access to the index in the stream? String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"}; List<String> nameList; Stream&l...
Animalcule asked 31/8, 2013 at 19:31

4

Solved

I have a Stream that does all processing in peek() methods. I don't need any result from the Stream, but I need a terminal operation in order for processing to occur. Of course I can terminate the ...
Swanner asked 17/2, 2015 at 14:47

4

Solved

I know how to create a Map<T, List<U>> , using Collectors.groupingBy: Map<Key, List<Item>> listMap = items.stream().collect(Collectors.groupingBy(s->s.key)); How would ...
Mesmerism asked 25/2, 2019 at 9:45

21

I am trying to list out duplicate elements in an integer list using Streams of JDK 8. For example: // Duplicates are {1, 4} List<Integer> numbers = Arrays.asList(new Integer[]{1,2,1,3,4,4}); ...
Onofredo asked 28/12, 2014 at 14:19

13

Solved

How to count the frequency of words of List in Java 8? List <String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao"); The result must be: {ciao=2, hello=1, bye=2} ...
Teodoor asked 18/3, 2015 at 12:43

© 2022 - 2024 — McMap. All rights reserved.