close
Skip to main content

New answers tagged

Score of 3
Accepted

How to understand and implement "get each worker tools" and then "get tools" in my Stream API test task for Java developer?

In your example you are doing steps 3.2 and 3.3 in a single stream operation. What you were asked was doing them in two separate operations. The end result was correct, but the way the task was ...
Score of 5

How to make this stream gatherer threadsafe?

So I am trying to understand how thread safety works with parallel stream For this, you should consult the API docs for the java.util.stream package, to which Gatherer belongs. Your initializer, ...
Score of 4

How to make this stream gatherer threadsafe?

Is my understanding correct or is the stream framework internally synchronizes access to the state objects while performing combiner and finisher operations? This is closest to the truth. Explicit ...
Score of 14

Why is Collectors.collect not able to infer the types in this stream pipeline?

The following code compiles and runs fine: List<String> letters = Arrays.asList("a", "b", "c", "d"); letters.stream().gather( Gatherer....
Score of 9

Why is Collectors.collect not able to infer the types in this stream pipeline?

The "obstacle" for the compiler to match the correct types in your example is the lambda. It does not specify its input types and Gatherer is declared with unlimited type parameters, so that ...
Score of 4

What does "must return a semantically identical result" mean?

What I am not sure what does it mean? As already mentioned by Slaw in the comments, it means that it shouldn't matter whether the Gatherer methods are called once or multiple times and which objects ...
Score of 0

How to combine two object lists by related field using the Stream API?

Unless you know that smallDataList is very small, I would convert it to a Map, and do a map lookup for each element of BigDataDao. Map<Integer, SmallDataDto> smallDataMap = smallDataList.stream()...
Score of 0

How to generate a stream of random booleans (or 2-valued enums)

One solution would be to use a BitSet with a byte array populated using RandomGenerator.nextBytes(byte[] bytes). In my testing this was about 30% faster than the approach used in the question. int ...
Score of 4

How to generate a stream of random booleans (or 2-valued enums)

The answer posed in the question was originally the fastest solution I found. It is also very straightforward and simple to understand. Stream<CoinFace> coinFlips = Stream.generate(...
Score of 1

How to generate a stream of random booleans (or 2-valued enums)

Here's a solution which uses the LongStream returned by RandomGenerator.longs(). This maps each of the 64 bits of each generated long to a CoinFace. Stream<CoinFace> coinFlips = RandomGenerator....
Score of 0

Check whether list of custom objects have same value for a property in Java 8

This is a somewhat verbose solution (unless you're reusing the Gatherer in different places in your code), but in Java 24 or later you could use a custom Gatherer to do this. class State { String ...
Score of 0

Transform a map<K, List<MyObject>> to map<K, List<String>> using the Stream API

If you want to use a third party solution, the StreamEx library provides support for streaming over Maps: Map<String, List<String>> departmentWiseEmployeeNames = EntryStream.of(...
Score of 2

Group by two fields and return elements in groups with multiple matches using the Stream API

record DeptAndSalary(String dept, Double salary) {} List<Employee> result = list.stream() .collect(Collectors.groupingBy(e -> new DeptAndSalary(e.getDept(), e.getSalary()))) ....
Score of 0

How to combine multiple Collections (Queue or List) element by element?

One approach would be to use Stream.generate to poll the next value from each of the queues and combine them, stopping using Stream.takeWhile when a null value is returned: Queue<Combined> ...
Score of 0

How to combine multiple Collections (Queue or List) element by element?

One way to look at this is that you want to zip together three queues. The Java Stream API doesn't have a zip method, but the StreamEx library does. Here's a StreamEx-based solution using zipWith to ...
Score of 0

Map<?, Optional<T>> to Map<?, T>

Here is a generic function which achieves the stated goal using Optional.stream and Stream.flatMap to unwrap the non-empty Optional values and skip the empty ones: private static <K, T> Map<K,...

Top 50 recent answers are included