java-stream

Java stream filter sum of values

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 23:30:48
问题 I have a class called MonitoredData with which describes an activity, its starting time and ending time. The attributes are activityLabel , startTime , endTime . I have to group and filter these activities, using streams, the activities which have the total duration of more than 10 hours. I managed to make the sum of durations and group them according to the activity using this: Map<String, Long> map4 = new HashMap<String, Long>(); map4 = data.stream() .collect( Collectors.groupingBy(

Performing specific operation on first element of list using Java8 streaming

*爱你&永不变心* 提交于 2019-12-06 22:16:25
问题 I want to perform certain operation on first element of my list and different operation for all remaining elements. Here is my code snippet: List<String> tokens = getDummyList(); if (!tokens.isEmpty()) { System.out.println("this is first token:" + tokens.get(0)); } tokens.stream().skip(1).forEach(token -> { System.out.println(token); }); Is there any more cleaner way to achieve this preferably using java 8 streaming API. 回答1: One way to express the intention is Spliterator<String> sp =

Check if java stream has been consumed

拟墨画扇 提交于 2019-12-06 21:24:23
问题 How can I check if a stream instance has been consumed or not (meaning having called a terminal operation such that any further call to a terminal operation may fail with IllegalStateException: stream has already been operated upon or closed. ? Ideally I want a method that does not consume the stream if it has not yet been consumed, and that returns a boolean false if the stream has been consumed without catching an IllegalStateException from a stream method (because using Exceptions for

How to enable “type information” for streams returned from methods?

巧了我就是萌 提交于 2019-12-06 21:24:22
问题 Since a few versions, IntelliJ has a very helpful feature: when you put the individual method calls of a stream() statement on separate lines, IntelliJ puts type information on each line: But when you don't call stream() directly, like when it is returned from another method, that information is omitted: Is there a way to convince IntelliJ to show such type information for such situations, too? As pure text, with manually inserted comments to "show" the problem with pure text: public Stream

Inclusive takeWhile() for Streams

半城伤御伤魂 提交于 2019-12-06 20:45:32
I want to know if there is a way to add the last element of the stream that was tested against the condition of the method takeWhile(). I believe I want to achieve something similar to RxJava's takeUntil() method. I'm guessing there is no direct way to do this (correct me if I am mistaken), but I would like to know if there is a proper workaround to achieve this that I am now aware of. I've searched throughout Stackoverflow with little to no success. If you think there are threads that could solve my problems, I would certainly like to see it. If you look at the following code's peek() you

Strange behavior of Stream.spliterator for parallel streams

最后都变了- 提交于 2019-12-06 20:15:58
问题 I'm using the stream spliterator directly for the low-level operations in the library I'm writing. Recently I discovered very weird behavior when I take the stream spliterator and interleave tryAdvance/trySplit calls. Here's a simple code which demonstrates the problem: import java.util.Arrays; import java.util.Spliterator; public class SpliteratorBug { public static void main(String[] args) { Integer[][] input = { { 1 }, { 2, 3 }, { 4, 5, 6 }, { 7, 8 }, { 9 } }; Spliterator<Integer>

Lowercase all HashMap keys

别等时光非礼了梦想. 提交于 2019-12-06 17:45:34
问题 I 've run into a scenario where I want to lowercase all the keys of a HashMap (don't ask why, I just have to do this). The HashMap has some millions of entries. At first, I thought I 'd just create a new Map, iterate over the entries of the map that is to be lowercased, and add the respective values. This task should run only once per day or something like that, so I thought I could bare this. Map<String, Long> lowerCaseMap = new HashMap<>(myMap.size()); for (Map.Entry<String, Long> entry :

Must partitioningBy produce a map with entries for true and false?

血红的双手。 提交于 2019-12-06 17:12:56
问题 The partitioningBy collector applies a predicate to each element in a stream and produces a map from booleans to lists of elements from the stream that satisfied or didn't satisfy the predicate. For instance: Stream.of(1,2,3,4).collect(partitioningBy(x -> x >= 3)) // {false=[1, 2], true=[3, 4]} As discussed in What's the purpose of partitioningBy, the observed behavior is that partitioningBy always returns a map with entries for both true and false . E.g.: Stream.empty().collect

How to stream a map with collection using Java 8 streams?

孤街浪徒 提交于 2019-12-06 16:29:30
问题 I'd like to stream a map with collection using Java 8 streams. For example, having the following data: Map<String, Collection<Integer>> data; I'd like to go over the elements handling each integer value with the corresponding key strings. For example: data.keyValueStream((k,v)-> ...) Any idea how to achieve this? Thanks. * Regarding the question "Why do you need it?", it could be a bunch of reasons and I'm not sure it's important. Anyhow, I'll "flow" with you... My specific scenario is to

Filter a list of strings which contains one or more strings from another list with Java 8 streams

雨燕双飞 提交于 2019-12-06 16:23:46
I want to use the strings imputed in a TextField to filter a list. I am using a KeyReleased Event on the TextField to filter the list on every key. The piece of code below filters the list when I type in a word, but when I press space and start typing another word the list gets empty. I am a bit new to streams. I don't know what I am doing wrong. private ObservableList<Products_Data> productList; @FXML private JFXTextField searchField; @FXML private TableView<Products_Data> productTable; @FXML void searchKeyReleased(KeyEvent event) { String searchText = searchField.getText(); List<String>