java-stream

Why can I collect a parallel stream to an arbitrarily large array but not a sequential stream?

北战南征 提交于 2019-12-21 07:26:17
问题 From answering this question, I ran into a peculiar feature. The following code works as I assumed it would (the first two values within the existing array would be overridden): Integer[] newArray = Stream.of(7, 8) .parallel() .toArray(i -> new Integer[] {1, 2, 3, 4, 5, 6}); System.out.println(Arrays.toString(newArray)); Output: [7, 8, 3, 4, 5, 6] However, attempting this with a sequential stream throws an IllegalStateException : Integer[] newArray = Stream.of(7, 8) .toArray(i -> new Integer[

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

独自空忆成欢 提交于 2019-12-21 07:25:37
问题 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:

Multiply 2 double[][] Matrices Using Streams

白昼怎懂夜的黑 提交于 2019-12-21 06:22:08
问题 I'm wondering what the most compact and efficient way to multiple 2 double[][] arrays matrices using streams. The approach should follow matrix multiplication rules as illustrated here: http://www.mathwarehouse.com/algebra/matrix/multiply-matrix.php Here's one way to do it using for loops ('this' is the first matrix'): final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); final double[][] outData = new double[nRows]

Multiply 2 double[][] Matrices Using Streams

房东的猫 提交于 2019-12-21 06:22:06
问题 I'm wondering what the most compact and efficient way to multiple 2 double[][] arrays matrices using streams. The approach should follow matrix multiplication rules as illustrated here: http://www.mathwarehouse.com/algebra/matrix/multiply-matrix.php Here's one way to do it using for loops ('this' is the first matrix'): final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); final double[][] outData = new double[nRows]

Stream closeable resource with Spring MVC

℡╲_俬逩灬. 提交于 2019-12-21 05:06:25
问题 After having read this article, I wish to use Spring to stream database query results directly to a JSON response to ensure constant-memory usage (no greedy loading of a List in memory). Similar to what is done in the article with Hibernate, I assembled a greetingRepository object which returns a stream of the database contents based on a JdbcTemplate . In that implementation, I create an iterator over the queried ResultSet , and I return the stream as follows: return StreamSupport.stream

What does the Java 8 Collector UNORDERED characteristic mean?

徘徊边缘 提交于 2019-12-21 04:28:08
问题 In official documentation you can read that: UNORDERED Indicates that the collection operation does not commit to preserving the encounter order of input elements. This is not too helpful without any examples. My question is, what exactly does UNORDERED characteristic mean? Should I use it with reducing collectors like min or sum or is it only applicable to collection collectors? In OpenJDK looks like reducing operations (min, sum, avg) have empty characteristics. I expected to find there at

What is the danger of side effects in Java 8 Streams?

£可爱£侵袭症+ 提交于 2019-12-21 04:26:31
问题 I'm trying to understand warnings I found in the Documentation on Streams. I've gotten in the habit of using forEach() as a general purpose iterator. And that's lead me to writing this type of code: public class FooCache { private static Map<Integer, Integer> sortOrderCache = new ConcurrentHashMap<>(); private static Map<Integer, String> codeNameCache = new ConcurrentHashMap<>(); public static void populateCache() { List<Foo> myThings = getThings(); myThings.forEach(thing -> { sortOrderCache

Create a nested parent child list in Java 8

送分小仙女□ 提交于 2019-12-21 04:08:14
问题 I am new to Java 8 and need a solution to the below problem. I have two classes as below: class Person { String name; int age; List<Address> address; } class Address { String street; String city; String country; } Now I have a list coming from database like this: List<Person> findPerson; adam 26 <123, xyz, yyy> adam 26 <456, rrr, kkk> bill 31 <666, uuu, hhh> Now I need to combine the same person objects with different address objects in one, like below? List<Person> findPerson; adam 26 <123,

How-to chain and apply a stream of comparators?

霸气de小男生 提交于 2019-12-21 04:06:07
问题 I have a stream of unsorted items and a stream of comparators. I want to apply all the comparators onto the stream by using "thenComparing" (Multisort) Is there a more elegant way than the following code to achive this? Stream unsorted = ...; Stream<Comparator> comparators = ...; Comparator compareFunc = comparators.reduce(null, (a, b) -> { if(a == null) { return b; }else { return a.thenComparing(b); } }); Stream result = unsorted.sorted(compareFunc); 回答1: Don’t use an identity value for

Time complexity of stream filter

寵の児 提交于 2019-12-21 04:02:25
问题 I have a code like this: List<Listing> Listings = new ArrayList<>(); Listings.add(listing1); Listings.add(listing2); ... ... ... Listing listing= listings.stream() .filter(l -> l.getVin() == 456) .findFirst(); My question is what is the time complexity of the filter process? If it is O(n), my intuition is to convert it into HashSet like data structures so that the time complexity could become O(1), Is there an elegant way to do this with streams ? 回答1: It is O(n) . The stream filtering uses