java-stream

What is the (kind of) inverse operation to Java's Stream.flatMap()?

老子叫甜甜 提交于 2019-12-10 03:49:21
问题 The Stream.flatMap() operation transforms a stream of a, b, c into a stream that contains zero or more elements for each input element, e.g. a1, a2, c1, c2, c3 Is there the opposite operations that batches up a few elements into one new one? It is not .reduce(), because this produces only one result It is not collect(), because this only fills a container (afaiu) It is not forEach(), because this has returns just void and works with side effects Does it exist? can I simulate it in any way?

Sort the outer map by the value of the nested map

爷,独闯天下 提交于 2019-12-10 00:32:54
问题 Sorting the outer map Map<String, Map<String, List<Integer>>> by the list size in the nested map retaining the outer and inner keys as before. 回答1: You may solve this by generalizing the process: private static <K,V,R> Map<K,R> replaceAndSortValues(Map<K,V> m, Function<V,R> f, Comparator<R> c) { return m.entrySet().stream() .map(e -> Map.entry(e.getKey(), f.apply(e.getValue()))) .sorted(Map.Entry.comparingByValue(c.reversed())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,

How to make stream pipeline simpler

跟風遠走 提交于 2019-12-10 00:32:47
问题 I think my code needs improvement. I use object allSummaryTSTLog both in the stream's filter() and map() stages, so I have to call File.listFiles twice: public static List<Test> ParserPath(List<String> allLogPath) { FilenameFilter filter = new MyFilter("Summary_TSTLog"); return allLogPath.parallelStream().filter(path -> { File testPath = new File(path); if (!testPath.isDirectory()) { MyLog.log.info("test path : [" + path + "] is not exist, continue"); return false; } File[] allSummaryTSTLog =

Collectors.groupingBy() returns result sorted in ascending order java

妖精的绣舞 提交于 2019-12-09 22:40:31
问题 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 LinkedHashMap<>(); try { myData = myService.getData(id); myid = myData.stream().collect(Collectors.groupingBy(myEntity::getDataId)); Here mydata is sorted by desc order but after creating collections by group data id my list get sorted with ascending order. I want my collection list to be descending order not ascending order. 回答1: As

Convert Map of maps into Lists using Java 8 Streams

我与影子孤独终老i 提交于 2019-12-09 19:43:22
问题 I have a map: Map<String, Map<Integer, List<Integer>>> e.g. Map<Name, Map<Id, List<ReferenceId>>> Outcome: List<Id> List<ReferenceId> I wanna convert this map into two list of Integers. One list contains inner-map keys, and other contains inner-map value (i.e. List<Integer> ) Can anyone tell me how to do this in Java 8 using streams? I tried this way but got Cast Exception, can not convert String to Integer. map.values().stream() .map(m -> m.entrySet() .stream() .map(e -> e.getKey()) .collect

A list contains at least one value from another list (Java 8)

一世执手 提交于 2019-12-09 18:33:58
问题 My function finds if from a list of given words at list one word is valid in the page. So when page contains words I have written it as follows: (Simplified version) private boolean atLeastOneWordIsValidInThePage(Page page, Set<Long> wordIdsToCheck) Set<Long> wordIds = page.getWords().stream() .filter(word -> word.isValid()) .map(word->getWordId()) .collect(Collectors.toSet()); return words.stream().anyMatch((o) -> wordIds.contains(o)); Is it the best java 8 practice to write it? I want to

Java 8 Stream - Why is filter method not executing? [duplicate]

烈酒焚心 提交于 2019-12-09 18:03:27
问题 This question already has answers here : Why does Java8 Stream generate nothing? (3 answers) Closed 3 years ago . I am learning filtering using java stream. But the stream after filtering is not printing anything. I think the filter method is not getting executed. My filtering code is as follows: Stream.of("d2", "a2", "b1", "b3", "c") .filter(s -> { s.startsWith("b"); System.out.println("filter: " + s); return true; }); There is no compilation error and no exception also. Any suggestion? 回答1:

Stream of cartesian product of other streams, each element as a List?

柔情痞子 提交于 2019-12-09 17:37:02
问题 How can I implement a function using Java 8 to take some number of streams, and produce a stream in which each element is a list consisting of one member of the Cartesian product of the streams? I've looked at this question -- that question uses an aggregator that is a BinaryOperator (taking two items of like type and producing an item of the same type). I'd like the items in the end result to be List s rather than the types of the elements in the input streams. Concretely, supposing my

How do I lazily concatenate streams?

戏子无情 提交于 2019-12-09 17:35:02
问题 I'm trying to implement a stream that uses another instance of itself in its implementation. The stream has a few constant elements prepended (with IntStream.concat) to it, so this should work as long as the concatenated stream creates the non-constant part lazily. I think using the StreamSupport.intStream overload taking a Supplier with IntStream.concat (which "creates a lazily concatenated stream") should be lazy enough to only create the second spliterator when elements are demanded from

Encounter order friendly/unfriendly terminal operations vs parallel/sequential vs ordered/unordered streams

北城以北 提交于 2019-12-09 16:45:21
问题 Inspired by this question, I started to play with ordered vs unordered streams, parallel vs sequential streams and terminal operations that respect encounter order vs terminal operations that don't respect it. In one answer to the linked question, a code similar to this one is shown: List<Integer> ordered = Arrays.asList( 1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4); List<Integer> result = new CopyOnWriteArrayList<>(); ordered.parallelStream().forEach(result::add); System.out