Java 8 stream: replace single item in streamed collection
Tag : java , By : Dominique Vocat
Date : March 29 2020, 07:55 AM
Hope this helps I'm somewhat new to working in Java 8, and am refactoring some older code with (what appears to be a good use case for) a stream operation. The older code "works" but to my eyes it looks really inefficient. , You can simply do: List<Object> newItems = items.stream()
.map(o -> o.getPropValue() == newObject.getPropValue() ? newObject : o)
.collect(toList());
putIntoCache(newObject.getKey(), newItems);
|
How to find the index of an element in a string collection using a stream in Java?
Tag : java , By : platformNomad
Date : March 29 2020, 07:55 AM
I hope this helps you . Whenever you need to use the loop index, the stream equivalent usually begins IntStream.range. In this case, the code looks like OptionalInt index = IntStream.range(0, re.length).filter(i -> re[i].equals(text)).findFirst();
int index = Arrays.asList(re).indexOf(text);
|
Java 8 Stream: How to compare current element with next element?
Tag : java , By : John Tate
Date : March 29 2020, 07:55 AM
|
Java 8 stream API: get first found collection element
Tag : java , By : DicksGarage
Date : March 29 2020, 07:55 AM
|
Iterate a collection then only one time execute stream concat all element of the collection
Date : March 29 2020, 07:55 AM
like below fixes the issue If you want a single Stream of all the elements of all the List s, you should use flatMap, not Stream.concat:Stream<User> users = map.values().stream().flatMap(List::stream);
List<User> userList = map.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
|