java-stream

converting for loop to java 8 stream

旧城冷巷雨未停 提交于 2020-01-03 13:03:11
问题 I was playing around with Java 8. I had some trouble converting this for loop into Java 8 Stream. for (int y = 0; y < 5; y ++) { for (int x = y; x < 10; x += 2) { System.out.println(x+y); } } Please help! 回答1: The canonical way of converting nested loops is to use flatMap on a stream, e.g. IntStream.range(0, 5).flatMap(i->IntStream.range(i, 10)) .forEach(System.out::println); The tricky part on your task is the increment by two as this has no direct equivalent in the stream API. There are two

Does reduce on an ordered stream reduce in order?

百般思念 提交于 2020-01-03 09:05:43
问题 I have a List of A, B, C. C reduce A reduce B != A reduce B reduce C (however, A reduce (B reduce C) is OK). In other words, my reduction operation is associative but not commutative. Does java enforce on an ordered sequential stream (such as the default one from a list) that reduction will always happen according to the encounter order? That is to say, will java reorder reductions (such that B reduce A instead of A reduce B)? (Hopefully this is clear enough). edit to add a little demo and

Using java streams to find if a number is prime or not

亡梦爱人 提交于 2020-01-03 04:53:07
问题 I am reading Cracking the Coding Interview and it has an example of finding prime number which I ran on JShell boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } then I am trying to convert this to streams in java, but finding this difficult as mentioned boolean isPrimeStream(int n) { return IntStream.range(0, n) // how to do similar to for loop above .anyMatch(i -> n % i == 0); // i think this is the reason for failure } 回答1:

Parallel message unmarshalling from a token delimited input stream with Java8 stream API

我的梦境 提交于 2020-01-03 03:14:45
问题 Is it possible with Java8 stream API to create interface like Message pullNext() plumbed on top of a delimited input stream with the logical steps as below? Delimited tokens are read from the character input stream Tokens fed to parallel unmarshaller (one token -> one Message) Messages are reordered back to retain the original incoming order Messages are pullable with aforementioned pullNext() Somewhat a disruptor with unmarshal stage served by concurrent pool. Similar to this, maybe

Java 2D array into 2D ArrayList with stream

吃可爱长大的小学妹 提交于 2020-01-02 13:44:32
问题 So I have an Integer[][] data that I want to convert into an ArrayList<ArrayList<Integer>> , so I tried using streams and came up with the following line: ArrayList<ArrayList<Integer>> col = Arrays.stream(data).map(i -> Arrays.stream(i).collect(Collectors.toList())).collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new)); But the last part collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new)) gives me an error that it cannot convert ArrayList<ArrayList<Integer>> to

sum of BigDecimal List using streams and sum method

时光怂恿深爱的人放手 提交于 2020-01-02 13:44:23
问题 If we have all int or long or other primitive datatype value in list then we obtain sun of all values using return items.stream().mapToInt(i -> i).sum(); I have list of BigDecimal values,how to find the sum of all values using Java8 As there is no default method like mapToBigDecimal I tried to create plain map but then I cannot use sum() 回答1: List<BigDecimal> items = Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(1.5), BigDecimal.valueOf(100)); items.stream().reduce(BigDecimal.ZERO,

Java Stream: difference between forEach and forEachOrdered

女生的网名这么多〃 提交于 2020-01-02 09:44:08
问题 Premise : I've already read this question and others, but I need some clarifications. I understand that Stream.forEach method makes the difference (not only) when dealing with parallel streams, and this explains why this //1 Stream.of("one ","two ","three ","four ","five ","six ") .parallel() .forEachOrdered(item -> System.out.print(item)); prints one two three four five six But when it comes to intermediate operations, the order is not guaranteed anymore when stream is parallelized. So this

Java 8 group by String

穿精又带淫゛_ 提交于 2020-01-02 08:04:15
问题 Here is my code: public class StudentData { public static List<Student> getData() { return Arrays.asList( new Student(1, "a1", 1, Arrays.asList("cricket", "football", "basketball")), new Student(2, "a2", 1, Arrays.asList("chess", "football")), new Student(3, "a3", 2, Arrays.asList("running")), new Student(4, "a4", 2, Arrays.asList("throwball", "football")), new Student(5, "a5", 3, Arrays.asList("cricket", "basketball")), new Student(6, "a6", 4, Arrays.asList("cricket")), new Student(7, "a7",

Java 8 group by String

拈花ヽ惹草 提交于 2020-01-02 08:03:09
问题 Here is my code: public class StudentData { public static List<Student> getData() { return Arrays.asList( new Student(1, "a1", 1, Arrays.asList("cricket", "football", "basketball")), new Student(2, "a2", 1, Arrays.asList("chess", "football")), new Student(3, "a3", 2, Arrays.asList("running")), new Student(4, "a4", 2, Arrays.asList("throwball", "football")), new Student(5, "a5", 3, Arrays.asList("cricket", "basketball")), new Student(6, "a6", 4, Arrays.asList("cricket")), new Student(7, "a7",

How to build a Map that replicates a Function in Java's Lambda API

梦想与她 提交于 2020-01-02 07:01:12
问题 From a java.util.function.BiFunction that maps a pair of Enum s into a value, I want to build a EnumMap that reflects that mapping. For instance, let E1 and E2 be enum types and T any given type: BiFunction<E1,E2, T> theBiFunction = //...anything EnumMap<E1,EnumMap<E2,T>> theMap = buildTheMap( // <-- this is where the magic happens E1.values(), E2.values(), theBiFunction); Given any pair of values of type E1 and E2 E1 e1 = //any valid value... E2 e2 = //any valid value.... both values below