java-stream

What are the reasons for not having an index in Java 8 streams?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 05:46:13
问题 I was wondering about the Java 8 streams ( Stream<E> ), they have the following methods: forEach(Consumer<? super E> action) forEachOrdered(Consumer<? super E> action) What were the arguments against not supplying the following signature? forEachOrdered(BiConsumer<Integer, ? super E> action) Which would then return the index of the item in the stream and the item itself. With this overload it would be possible to actually use the index in case the stream was ordered. I am really curious to

How to convert a String to a Java 8 Stream of Characters?

一世执手 提交于 2019-12-18 05:29:05
问题 I found this question about getting a java.util.streams.IntStream from a java String but I have not found this method now that I'm using Java 8. Correction: As you guys pointed, I was using Java 7. Now the method chars() is there. But the question still applies: How can I get a Stream<Character> from a String? 回答1: I was going to point you to my earlier answer on this topic but it turns out that you've already linked to that question. The other answer also provides useful information. If you

Is it possible to .flatMap() or .collect() evenly multiple collections

心不动则不痛 提交于 2019-12-18 04:48:07
问题 For instance there are collections [1,2,3,4,5] , [6,7,8] , [9,0] . Any way to avoid loops with iterators to interleave these collections via Java 8 stream API to get the following result - [1,6,9,2,7,0,3,8,4,5] ? 回答1: I am not sure if there's a simpler way with the Stream API, but you can do this using a stream over the indices of all the lists to consider: static <T> List<T> interleave(List<List<T>> lists) { int maxSize = lists.stream().mapToInt(List::size).max().orElse(0); return IntStream

Why is parallel stream slower?

淺唱寂寞╮ 提交于 2019-12-18 04:25:07
问题 I was playing around with infinite streams and made this program for benchmarking. Basically the bigger the number you provide, the faster it will finish. However, I was amazed to find that using a parellel stream resulted in exponentially worse performance compared to a sequential stream. Intuitively, one would expect an infinite stream of random numbers to be generated and evaluated much faster in a multi-threaded environment, but this appears not to be the case. Why is this? final int

Is Java 8 streams atomic?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 04:23:39
问题 I read a few posts, however, I'm still confused. I know that parallel streams will be executed in a parallel manner that will utilise the CPUs. and I believe that the sub jobs will be executed as atomic units, am I correct? But what about regular Java 8 streams? If i execute let's say the next line of code: users.stream().map(user->user.getUsername()).collect(Collectors.toList()); Will that line be executed in a thread-safe/atomic manner as well? 回答1: There is no such thing as general thread

how to use java 8 stream and lambda to flatMap a groupingBy result

血红的双手。 提交于 2019-12-18 04:21:58
问题 I have a object with contains a list of other objects and I want to return a flatmap of the contained objects mapped by some property of the container. Any one if is it possible using stream and lambdas only? public class Selling{ String clientName; double total; List<Product> products; } public class Product{ String name; String value; } Lets supose a list of operations: List<Selling> operations = new ArrayList<>(); operations.stream() .filter(s -> s.getTotal > 10) .collect(groupingBy

Java 8 grouping by from one-to-many

*爱你&永不变心* 提交于 2019-12-18 03:35:17
问题 I want to learn how to use the Java 8 syntax with streams and got a bit stuck. It's easy enough to groupingBy when you have one key for every value. But what if I have a List of keys for every value and still want to categorise them with groupingBy? Do I have to break it into several statements or is there possibly a little stream magic that can be done to make it simpler. This is the basic code: List<Album> albums = new ArrayList<>(); Map<Artist, List<Album>> map = albums.stream().collect

How to print two lists together using Stream API java 8?

非 Y 不嫁゛ 提交于 2019-12-18 03:32:42
问题 I have two lists as follow List<String> names = Arrays.asList("James","John","Fred"); List<Integer> ages = Arrays.asList(25,35,15); What i want to do is to print those two lists like so James:25 John:35 Fred:15 It is easy to do it using the classic way for(int i=0;i<names.size();i++){ System.out.println(names.get(i)+":"+ages.get(i)); } Is there a way to do it using Stream API java 8? What i am able to do is to print only one single list names.stream().forEach(System.out::println); 回答1: The

How to print two lists together using Stream API java 8?

∥☆過路亽.° 提交于 2019-12-18 03:32:07
问题 I have two lists as follow List<String> names = Arrays.asList("James","John","Fred"); List<Integer> ages = Arrays.asList(25,35,15); What i want to do is to print those two lists like so James:25 John:35 Fred:15 It is easy to do it using the classic way for(int i=0;i<names.size();i++){ System.out.println(names.get(i)+":"+ages.get(i)); } Is there a way to do it using Stream API java 8? What i am able to do is to print only one single list names.stream().forEach(System.out::println); 回答1: The

Confused by Java8 Collectors.toMap

假装没事ソ 提交于 2019-12-18 03:01:42
问题 I have a collection that looks like below, and I want to filter out the everything except the dates that aren't the end of the months. 2010-01-01=2100.00, 2010-01-31=2108.74, 2010-02-01=2208.74, 2010-02-28=2217.92, 2010-03-01=2317.92, 2010-03-31=2327.57, 2010-04-01=2427.57, 2010-04-30=2437.67, 2010-05-01=2537.67, 2010-05-31=2548.22, 2010-06-01=2648.22, 2010-06-30=2659.24, 2010-07-01=2759.24, 2010-07-31=2770.72, 2010-08-01=2870.72, 2010-08-31=2882.66, 2010-09-01=2982.66, 2010-09-30=2995.07,