java-stream

using a stream based on the content of Optional<Map>

冷暖自知 提交于 2019-12-22 06:46:44
问题 I get a map from a service not under my control that might be null and want to process it, let's say, filter, map and reduce to a single element I need. Question: is there a "link" from Optional to Stream? I tried (among other things): return Optional.ofNullable(getMap()) .map(Map::entrySet) // gets the entryset .map(Stream::of) .orElseGet(Stream::empty) // i would then like to continue with .filter(e -> e.getKey().startsWith("f") .map(Entry::getValue) .findFirst(); but then I get not Stream

using a stream based on the content of Optional<Map>

余生长醉 提交于 2019-12-22 06:46:10
问题 I get a map from a service not under my control that might be null and want to process it, let's say, filter, map and reduce to a single element I need. Question: is there a "link" from Optional to Stream? I tried (among other things): return Optional.ofNullable(getMap()) .map(Map::entrySet) // gets the entryset .map(Stream::of) .orElseGet(Stream::empty) // i would then like to continue with .filter(e -> e.getKey().startsWith("f") .map(Entry::getValue) .findFirst(); but then I get not Stream

Javascript equivalents for Java Streams API

一世执手 提交于 2019-12-22 06:40:27
问题 I like the Java 8's streaming API. There are plenty of useful intermediate and terminal methods to transform and collect the stream. I'm talking about intermediate methods like distinct() or terminal methods like collect() . I find the Collector API especially useful, to reduce the stream to deep grouping maps. What is the javascript equivalent for the Java streaming API? I know there're basic functions like map , filter and reduce , but don't find any more generalized interfaces provided by

Java 8 stream for-loop

这一生的挚爱 提交于 2019-12-22 05:25:26
问题 Im new to Java 8 Streams and would like to convert following code-block to Java 8's Stream way of doing the same thing. Edit : Updates the class-names to be less confusing. (Removed Foo, Bar, Baz...) ArrayList<PriceList> priceLists = new ArrayList<PriceList>(); // I'm casting to a type-safe List from getObjects() // -which is a function I dont have access to. Is there a nice // solution to embed this in the stream-syntax? List<PriceListGroup> distObjects = (List<PriceListGroup>) objects.get(1

Logging the result of filter while using java streams filter by predicate

不羁岁月 提交于 2019-12-22 05:23:33
问题 The scenario is there are different types of filters I have created which filters the list of some objects based on the property of the object. So for that I have created an AbstractObjectFilter class which inherited by every filter. AbstractObjectFilter.java public abstract class AbstractEventFilter { protected abstract Predicate<IEvent> isEligible(); public List<IEvent> getFilteredEvents(final List<IEvent> events) { return events.stream().filter(isEligible()).collect(Collectors.toList()); }

How to restrict a Stream to run sequentially, and prevent it from running in parallel?

孤者浪人 提交于 2019-12-22 05:22:08
问题 I have a method that returns a stream that is generated from a custom spliterator; the spliterator is not tread safe. Since the spliterator is not tread safe, and it maintains state, I want to prevent it from running in parallel. Is there a way to prevent the returned stream from running in parallel? I have not been able to find any documentation or examples that do this. I did find a sequential() method on the BaseStream class, but that does not appear to prevent a user from then calling

Convert `BufferedReader` to `Stream<String>` in a parallel way

∥☆過路亽.° 提交于 2019-12-22 05:10:44
问题 Is there a way to receive a Stream<String> stream out of a BufferedReader reader such that each string in stream represents one line of reader with the additional condition that stream is provided directly (before reader read everything)? I want to process the data of stream parallel to getting them from reader to save time. Edit: I want to process the data parallel to reading. I don't want to process different lines parallel. They should be processed in order. Let's make an example on how I

stream creating List of List (nested List) using forEach, Java 8

邮差的信 提交于 2019-12-22 05:08:11
问题 class EntityCompositeId { private Long firstId; private Long secondId; // getter & setter... } class EntityComposite { private EntityCompositeId id; private String first; private String second; // getter & setter... } List<EntityComposite> listEntityComposite = .... Supose this content 1, 1, "firstA", "secondBirdOne" 1, 2, "firstA", "secondBirdTwo" 1, 3, "firstA", "secondBirdThree" 2, 1, "firstB", "secondCatOne" 2, 2, "firstB", "secondCatTwo" 2, 3, "firstB", "secondCatThree" 3, 1, "firstC",

Map to a running sum in Java 8

醉酒当歌 提交于 2019-12-22 04:43:15
问题 If I have a collection: List<Long> numbers = asList(2, 2, 4, 5); How can I map/process these to build up a running total. To produce something like: List<Long> runningTotals = asList(2, 4, 8, 13); Even better, how can I build a list of something (like a tuple) so I can preserve the orignals: ((2 -> 2), (2 -> 4), (4 -> 8), (5 -> 13)); 回答1: Update : As Holger pointed out in the comments using Stream.reduce() for this purpose is not correct. See Reduction and Mutable Reduction or Java 8 Streams

Java Lambda - check if an ArrayList to Stream is empty

流过昼夜 提交于 2019-12-22 04:12:57
问题 I have the following lambda expression and if works fine when bonusScheduleDurationContainers is not empty. If it is empty, I get a NoSuchElementException . How do I check this in the lambda expression? final List<ScheduleDurationContainer> bonusScheduleDurationContainers = scheduleDurationContainersOfWeek.stream() .filter(s -> s.getContainerType() == ScheduleIntervalContainerTypeEnum.BONUS) .collect(Collectors.toList()); final ScheduleDurationContainer bonusScheduleDurationContainer =