java-stream

Why overload the varargs method of() in Java Stream interface?

可紊 提交于 2019-12-29 00:08:16
问题 The Stream interface has two overloads for the method of() . One of these is a variable-arity method while the other takes a single argument. Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of() . I see that the implementation differs between these methods, with the

Java 8 Stream: difference between limit() and skip()

不打扰是莪最后的温柔 提交于 2019-12-28 11:44:51
问题 Talking about Stream s, when I execute this piece of code public class Main { public static void main(String[] args) { Stream.of(1,2,3,4,5,6,7,8,9) .peek(x->System.out.print("\nA"+x)) .limit(3) .peek(x->System.out.print("B"+x)) .forEach(x->System.out.print("C"+x)); } } I get this output A1B1C1 A2B2C2 A3B3C3 because limiting my stream to the first three components forces actions A , B and C to be executed only three times. Trying to perform an analogous computation on the last three elements

Java 8 Stream: difference between limit() and skip()

喜欢而已 提交于 2019-12-28 11:44:34
问题 Talking about Stream s, when I execute this piece of code public class Main { public static void main(String[] args) { Stream.of(1,2,3,4,5,6,7,8,9) .peek(x->System.out.print("\nA"+x)) .limit(3) .peek(x->System.out.print("B"+x)) .forEach(x->System.out.print("C"+x)); } } I get this output A1B1C1 A2B2C2 A3B3C3 because limiting my stream to the first three components forces actions A , B and C to be executed only three times. Trying to perform an analogous computation on the last three elements

ConcurrentModificationException when using stream with Maps key set

送分小仙女□ 提交于 2019-12-28 06:33:05
问题 I want to remove all items from someMap which keys are not present in someList . Take a look into my code: someMap.keySet().stream().filter(v -> !someList.contains(v)).forEach(someMap::remove); I receive java.util.ConcurrentModificationException . Why? Stream is not parallel. What is the most elegant way to do this? 回答1: @Eran already explained how to solve this problem better. I will explain why ConcurrentModificationException occurs. The ConcurrentModificationException occurs because you

How to get a Stream from a float[]

痴心易碎 提交于 2019-12-28 05:57:08
问题 I was learning how to use java 8 streams when I noticed something weird. Arrays.stream() has methods for everything but float arrays : Arrays.stream(int[]) : IntStream Arrays.stream(long[]) : LongStream Arrays.stream(double[]) : DoubleStream Similarly, there are Stream implementations for int, double etc but not floats : IntStream LongStream DoubleStream Is there a reason for that? what is the recommended way to work with float streams? 回答1: from Java SE 8 for the Really Impatient by Cay S.

How to get a Stream from a float[]

馋奶兔 提交于 2019-12-28 05:57:02
问题 I was learning how to use java 8 streams when I noticed something weird. Arrays.stream() has methods for everything but float arrays : Arrays.stream(int[]) : IntStream Arrays.stream(long[]) : LongStream Arrays.stream(double[]) : DoubleStream Similarly, there are Stream implementations for int, double etc but not floats : IntStream LongStream DoubleStream Is there a reason for that? what is the recommended way to work with float streams? 回答1: from Java SE 8 for the Really Impatient by Cay S.

Convert String array to Map using Java 8 Lambda expressions

我怕爱的太早我们不能终老 提交于 2019-12-28 05:56:35
问题 Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map using the Java 8 lambda syntax? Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":") .collect(Collectors.toMap(keyMapper?, valueMapper?)); The solution I have right now does not seem really functional: Map<String, Double> kvs = new HashMap<>(); Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":")) .forEach(elem -> kvs.put(elem[0], Double

Java 8 Collectors.toMap SortedMap

佐手、 提交于 2019-12-28 04:59:42
问题 I'm using Java 8 lambdas and want to use Collectors toMap to return a SortedMap . The best I can come up with is to call the following Collectors toMap method with a dummy mergeFunction and mapSupplier equal to TreeMap::new . public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) { BiConsumer<M, T> accumulator = (map, element) ->

java 8 - stream, map and count distinct

坚强是说给别人听的谎言 提交于 2019-12-28 04:12:05
问题 My first attempt with java 8 streams... I have an object Bid, which represents a bid of a user for an item in an auction. i have a list of bids, and i want to make a map that counts in how many (distinct) auctions the user made a bid. this is my take on it: bids.stream() .collect( Collectors.groupingBy( bid -> Bid::getBidderUserId, mapping(Bid::getAuctionId, Collectors.toSet()) ) ).entrySet().stream().collect(Collectors.toMap( e-> e.getKey(),e -> e.getValue().size()) ); It works, but i feel

Java 8 Stream and operation on arrays

不打扰是莪最后的温柔 提交于 2019-12-28 03:23:29
问题 I have just discovered the new Java 8 stream capabilities. Coming from Python, I was wondering if there was now a neat way to do operations on arrays like summing, multiplying two arrays in a "one line pythonic" way ? Thanks 回答1: There are new methods added to java.util.Arrays to convert an array into a Java 8 stream which can then be used for summing etc. int sum = Arrays.stream(myIntArray) .sum(); Multiplying two arrays is a little more difficult because I can't think of a way to get the