java-stream

Java sum two double[][] with parallel stream

你离开我真会死。 提交于 2019-12-18 18:53:36
问题 Let's say I have this two matrices: double[][] a = new double[2][2] a[0][0] = 1 a[0][1] = 2 a[1][0] = 3 a[1][1] = 4 double[][] b = new double[2][2] b[0][0] = 1 b[0][1] = 2 b[1][0] = 3 b[1][1] = 4 in the traditional way, to sum this matrices I would do a nested for loop: int rows = a.length; int cols = a[0].length; double[][] res = new double[rows][cols]; for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ res[i][j] = a[i][j] + b[i][j]; } } I'm fairly new to the stream API but I

Collectors.groupingBy doesn't accept null keys

拥有回忆 提交于 2019-12-18 18:35:31
问题 In Java 8, this works: Stream<Class> stream = Stream.of(ArrayList.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); But this doesn't: Stream<Class> stream = Stream.of(List.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); Maps allows a null key, and List.class.getSuperclass() returns null. But Collectors.groupingBy emits a NPE, at Collectors.java, line 907: K key =

Is there any way for reading two or more files in one Java8-stream?

妖精的绣舞 提交于 2019-12-18 16:52:17
问题 I like new Java8 StreamAPI and want use it not only for one file. As usually, I use this code: Stream<String> lines = Files.lines(Paths.get("/somepathtofile")); But how read two file in one stream if it possibly? 回答1: Without any extra helper functions or outside libraries, the easiest is: Stream<String> lines1 = Files.lines(Paths.get("/somepathtofile")); Stream<String> lines2 = Files.lines(Paths.get("/somepathtoanotherfile")); Stream.concat(lines1, lines) .filter(...) .forEach(...); If Files

Sorting string value in a case-insensitive manner in Java 8

喜欢而已 提交于 2019-12-18 14:17:18
问题 How do I sort string values in case-insensitive order in the following? List<Employee> listofEmployees = Arrays.asList( new Employee(1, "aaa", Arrays.asList(123, 345, 678)), new Employee(1, "bbb", Arrays.asList(91011, 121314, 1516117)), new Employee(2, "ccc", Arrays.asList(181920, 212223, 242526)), new Employee(3, "ddd", Arrays.asList(272829, 303132, 333435)), new Employee(4, "BBB", Arrays.asList(29, 332, 33)) ); I wrote like this: listofEmployees.stream().sorted(Comparator.comparing(Employee

Java 8 lambda filtering based on condition as well as order

邮差的信 提交于 2019-12-18 14:14:17
问题 I was trying to filter a list based on multiple conditions, sorting. class Student{ private int Age; private String className; private String Name; public Student(int age, String className, String name) { Age = age; this.className = className; Name = name; } public int getAge() { return Age; } public void setAge(int age) { Age = age; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getName() { return

Convert a String to a java.util.Stream<Character>

巧了我就是萌 提交于 2019-12-18 13:17:21
问题 Sometimes I want to do something simple with each character in a string. Unfortunately, because a string is immutable, there is no good way of doing it except looping through the string which can be quite verbose. If you would use a Stream instead, it could be done much shorter, in just a line or two. Is there a way to convert a String into a Stream<Character> ? 回答1: You can use chars() method provided in CharSequence and since String class implements this interface you can access it. The

Java - Stream - Collect every N elements

那年仲夏 提交于 2019-12-18 12:16:47
问题 I am trying to learn java - stream. I am able to do simple iteration / filter / map / collection etc. When I was kind of trying to collect every 3 elements and print as shown here in this example, [collect every 3 elements and print and so on...] List<String> list = Arrays.asList("a","b","c","d","e","f","g","h","i","j"); int count=0; String append=""; for(String l: list){ if(count>2){ System.out.println(append); System.out.println("-------------------"); append=""; count=0; } append = append

Why is `Stream.collect` type-safe and `Stream.toArray(IntFunction<A[]>)` is not?

时间秒杀一切 提交于 2019-12-18 12:15:44
问题 Consider the following code fragment String strings[] = {"test"}; final List<String> collect = java.util.Arrays.stream(strings).collect(java.util.stream.Collectors.toList()); final Double[] array = java.util.Arrays.stream(strings).toArray(Double[]::new); Why can Java guarantee the correct type in the collect-case (changing the generic type of collect to e.g. Double leads to a compile time error), but not in the array case (compiles fine, despite apply(int) of Double[]::new gives a Double[] ,

How to perform Stream functions on an Iterable? [duplicate]

元气小坏坏 提交于 2019-12-18 12:06:06
问题 This question already has answers here : Why does Iterable<T> not provide stream() and parallelStream() methods? (3 answers) Closed 5 years ago . In Java 8, the Stream class does not have any method to wrap a an Iterable . Instead, I am obtaining the Spliterator from the Iterable and then obtaining a Stream from StreamSupport like this: boolean parallel = true; StreamSupport.stream(spliterator(), parallel) .filter(Row::isEmpty) .collect(Collectors.toList()) .forEach(this::deleteRow); Is there

How does the reduce() method work in Java 8?

試著忘記壹切 提交于 2019-12-18 11:23:47
问题 I try to understand how does the reduce() method work in java-8. For example I have this code: public class App { public static void main(String[] args) { String[] arr = {"lorem", "ipsum", "sit", "amet"}; List<String> strs = Arrays.asList(arr); int ijk = strs.stream().reduce(0, (a, b) -> { System.out.println("Accumulator, a = " + a + ", b = " + b); return a + b.length(); }, (a, b) -> { System.out.println("Combiner"); return a * b; }); System.out.println(ijk); } } And the output is this: