java-stream

Java 8 stream processing not fluent [closed]

匆匆过客 提交于 2019-12-04 02:13:37
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I have a problem with Java 8 streams, where the data is processed in sudden bulks, rather than when they are requested. I have a rather complex stream-flow which has to be parallelised because I use concat to merge two streams. My issue stems from the fact that data seems to be parsed in large

How to add dateTime values in a stream(java)?

好久不见. 提交于 2019-12-04 02:04:24
问题 Today I started learning Java 8, so I'm quite new to Java 8 and it's stuff. I have a list of activities. An activity has name, startTime, endTime. For startTime and endTime i'm using DateTime from joda time. I'm trying to determine a data structure of the form Map (String, DateTime) that maps for each activity the total duration computed over the monitoring period. Here is a snippet of my code: import java.util.ArrayList; import java.util.Map; import org.joda.time.DateTime; class Activity {

Collect stream with grouping, counting and filtering operations

做~自己de王妃 提交于 2019-12-04 02:03:51
I'm trying to collect stream throwing away rarely used items like in this example: import java.util.*; import java.util.function.Function; import static java.util.stream.Collectors.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import org.junit.Test; @Test public void shouldFilterCommonlyUsedWords() { // given List<String> allWords = Arrays.asList( "call", "feel", "call", "very", "call", "very", "feel", "very", "any"); // when Set<String> commonlyUsed = allWords.stream() .collect(groupingBy(Function.identity(), counting()))

Multiple “match” checks in one stream

拟墨画扇 提交于 2019-12-04 01:42:18
Is it possible to check if an array (or collection) contains element 5 and element other than 5. In one stream returning boolean result instead of using two streams: int[] ints = new int[]{1, 2, 3, 4, 5}; boolean hasFive = IntStream.of(ints).anyMatch(num -> num == 5); boolean hasNonFive = IntStream.of(ints).anyMatch(num -> num != 5); boolean result = hasFive && hasNonFive; Here's two solutions involving my StreamEx library. The core feature I'm using here is the concept of short-circuiting collectors. My library enhances the Collector concept to provide the ability to short-circuit (which

What is the best way to convert a byte array to an IntStream?

徘徊边缘 提交于 2019-12-04 01:36:31
Java 8 has java.util.stream.Stream and java.util.stream.IntStream types. java.util.Arrays has a method IntStream is = Arrays.stream(int[]) but no such method to make an IntStream from a byte[], short[] or char[], widening each element to an int. Is there an idiomatic/preferred way to create an IntStream from a byte[], so I can operate on byte arrays in a functional manner? I can of course trivially convert the byte[] to int[] manually and use Arrays.stream(int[]), or use IntStream.Builder: public static IntStream stream(byte[] bytes) { IntStream.Builder isb = IntStream.builder(); for (byte b:

Is Java 8 findFirst().isPresent() more efficient than count() > 0?

我与影子孤独终老i 提交于 2019-12-04 01:27:07
Given I have a stream Stream<T> stream = list.stream().filter(some predicate) where the list is very large, is it more efficient to check whether the stream is non-empty by doing: stream.count() > 0 or by doing: stream.findFirst().isPresent() ? If all you want to know, is whether there is a match, you should use list.stream().anyMatch(some predicate) , not only because it is more efficient, but also, because it is the correct idiom for expressing you intention. As said by others, anyMatch is short-circuiting, which implies that it will stop at the first match, whereas count will, as the name

Is there Java stream equivalent to while with variable assignment

大憨熊 提交于 2019-12-04 01:17:18
问题 Is there any stream equivalent to the following List<Integer> ints; while (!(ints = this.nextInts()).isEmpty()) { // do work } 回答1: first, thanks for the @Olivier Grégoire comments. it change my answer to a new knowledge. write your own Spliterator for the unknown size nextInts , then you can using StreamSupport#stream to create a stream for nextInts . for example: generateUntil(this::nextInts, List::isEmpty).forEach(list -> { //do works }); import static java.util.stream.StreamSupport.stream

Word count with java 8

最后都变了- 提交于 2019-12-04 01:11:47
问题 I am trying to implement a word count program in java 8 but I am unable to make it work. The method must take a string as parameter and returns a Map<String,Integer> . When I am doing it in old java way, everthing works fine. But when I am trying to do it in java 8, it returns a map where the keys are the empty with the correct occurrences. Here is my code in a java 8 style : public Map<String, Integer> countJava8(String input){ return Pattern.compile("(\\w+)").splitAsStream(input).collect

Why is this usage of Stream::flatMap wrong?

徘徊边缘 提交于 2019-12-03 23:39:36
问题 I expected to be able to use Stream::flatMap like this public static List<String> duplicate(String s) { List<String> l = new ArrayList<String>(); l.add(s); l.add(s); return l; } listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList()); But I get the following compiler error Test.java:25: error: incompatible types: cannot infer type-variable(s) R listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList()); (argument mismatch; bad return type in

Limit groupBy in Java 8

社会主义新天地 提交于 2019-12-03 23:21:42
How can I limit groupBy by each entry? For example (based on this example: stream groupBy ): studentClasses.add(new StudentClass("Kumar", 101, "Intro to Web")); studentClasses.add(new StudentClass("White", 102, "Advanced Java")); studentClasses.add(new StudentClass("Kumar", 101, "Intro to Cobol")); studentClasses.add(new StudentClass("White", 101, "Intro to Web")); studentClasses.add(new StudentClass("White", 102, "Advanced Web")); studentClasses.add(new StudentClass("Sargent", 106, "Advanced Web")); studentClasses.add(new StudentClass("Sargent", 103, "Advanced Web")); studentClasses.add(new