java-stream

Java - Read a big file (few GB) without exception

99封情书 提交于 2019-12-14 04:24:25
问题 This question is very short. I have a File Datei.trec-3,99 GB and i read it with this code: public class Main { public static void main(String[] args) { byte[] content = null; try { content = Files.readAllBytes(Paths.get("D:", "Videos","Captures","Datei.trec")); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } and this is the output: Exception in thread "main" java.lang.OutOfMemoryError: Required array size too large at java.nio.file.Files.readAllBytes(Unknown

Finding an odd number from int[] array using Stream

a 夏天 提交于 2019-12-14 03:48:26
问题 I created an array and trying to use streams for finding the first odd number as follows: int[] arr = new int[]{2, 4, 6, 8, 9, 12}; int oddOne = Stream.of(arr).filter(i -> i % 2 != 0).findFirst().get(); // fails above Error: | incompatible types: int[] cannot be converted to int What am I doing wrong? How do I fix this? 回答1: You need to use Arrays.stream(): Arrays.stream(arr).filter(i -> i % 2 != 0).findFirst().getAsInt(); Which: Returns a sequential IntStream with the specified array as its

Java 8 stream for Map <String, Set<String>>

你离开我真会死。 提交于 2019-12-14 03:44:47
问题 I have a Map< Integer, Set < Integer > >. I would like to convert it to a list of Integers based on some modification done by a custom method. Right now I am using two for loops and I wanted to know if there's a better way to do it using java streams Here's my existing code: public myMethod(Map<Integer, Set<Integer>> myMap, String a, int b) { List<Integer> myIntegerList = new ArrayList<>(); for (int i: myMap.keySet()) { for ( int j: myMap.get(i)) { myIntegerList.add(myCustomMethod(i, j, a

Grouping by in Java 8 stream to custom class rather than the origin class

落花浮王杯 提交于 2019-12-13 22:40:43
问题 I have a class A which has some fields. Class A{ String type; String x; String y; } Class B{ String x; String y; } Let's say we have a list List<A> . By using Collectors.groupingBy() , is it possible to get output Map<String,List<B>> instead Map<String,List<A>> ? where key in the Map is type field in Class A . 回答1: Of course - just chain a mapping() collector to the groupingBy() collector. Map<String,List<B>> map = listA.stream() .collect(Collectors.groupingBy(A::getType, Collectors.mapping(a

StreamEx grouping into lists returns an incorrect number of records

帅比萌擦擦* 提交于 2019-12-13 18:34:08
问题 The following code splits a stream of objects into chunks of 1000, processes them on materialisation and returns the total number of objects at the end. In all cases the number returned is correct unless the stream size happens to be 1. In the case the stream size is 1, the number returned is 0. Any help would be greatly appreciated. I have also had to hack the return call in the case there are no records in the stream to be 0. I'd like to fix this too. AtomicInteger recordCounter = new

get nested List of CustomObject from JTree, including leaf

寵の児 提交于 2019-12-13 17:24:02
问题 Get List<List<CustomObject>> from JTree I was populating the JTree according to this question... Recursively populate JTree, using list of list using the Answer https://stackoverflow.com/a/56090968/7339033 List<List<Pair>> listOfListPair = new ArrayList<>(); listOfListPair.add((List<Pair>) Arrays.asList( new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)})); listOfListPair.add((List<Pair>) Arrays.asList( new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)})); listOfListPair.add(

Populating a List with a contiguous range of shorts

不打扰是莪最后的温柔 提交于 2019-12-13 17:21:37
问题 https://stackoverflow.com/a/23675131/14731 provides a nice solution for generating a list of contiguous integers. Seeing as JDK8 does not provide a ShortStream class, how would you generate a list of contiguous shorts? I'm looking for something along the lines of: List<Short> range = ShortStream.range(0, 500).boxed().collect(Collectors.toList()); where the output contains a list of shorts, from 0 to 500 inclusive. 回答1: List<Short> range = IntStream.range(0, 500).mapToObj(i -> (short) i)

Spring Data concurrency

旧巷老猫 提交于 2019-12-13 15:32:31
问题 I want to get data from H2 database concurrently. I wanted to do it through parallel stream, but I am getting wrong results with parallel stream. With normal stream it returns right result (only one SELECT returns result others returns null). I am not able to find where is the problem. I look like in concurrent access to repository. Can you help? Entity: @Entity public class Car { @Id private String carType; public String getCarType() { return carType; } public void setCarType(String carType)

Java 8 Iterator to stream to iterator causes redundant call to hasNext()

人走茶凉 提交于 2019-12-13 14:29:06
问题 I notice a bit of a strange behavior in the following scenario: Iterator -> Stream -> map() -> iterator() -> iterate The hasNext() of the original iterator is called an additional time after having already returned false. Is this normal? package com.test.iterators; import java.util.Iterator; import java.util.Spliterators; import java.util.stream.Stream; import java.util.stream.StreamSupport; public class TestIterator { private static int counter = 2; public static void main(String[] args) {

Java streams to map

夙愿已清 提交于 2019-12-13 12:32:29
问题 I'm having a problem with the Map<Integer, List<String>> personenPerLeeftijd , the compiler says the method, Persoon::getLeeftijd, cannot be resolved. I don't really know what else I can do, sorry for the Dutch words! I you need any more info pls ask so public class TestPersoon2 { public static void main(String[] args) { final List<Persoon> personen = Personen.getPersonen(); Map<String, Persoon> map = personen.stream().collect(Collectors.toMap(p -> p.getNaam() + "-" + p.getLeeftijd(), p -> p)