java-stream

Is it possible to do a lazy groupby, returning a stream, in java 8?

孤人 提交于 2019-12-05 09:13:42
I have some large-ish text files that I want to process by grouping its lines. I tried to use the new streaming features, like return FileUtils.readLines(...) .parallelStream() .map(...) .collect(groupingBy(pair -> pair[0])); The problem is that, AFAIK, this generates a Map. Is there any way to have high level code like the one above that generates, for example, a Stream of Entries? UPDATE : What I'm looking for is something like python's itertools.groupby . My files are already sorted (by pair[0]), I just want to load the groups one by one. I already have an iterative solution. I'm just

filter Map in Java 8 Streams

元气小坏坏 提交于 2019-12-05 09:11:00
I was trying to filter Entries in HashMap using Streams API, but stuck in last method call Collectors.toMap . So, I don't have clue to implemement toMap method public void filterStudents(Map<Integer, Student> studentsMap){ HashMap<Integer, Student> filteredStudentsMap = studentsMap.entrySet().stream(). filter(s -> s.getValue().getAddress().equalsIgnoreCase("delhi")). collect(Collectors.toMap(k , v)); } public class Student { private int id; private String firstName; private String lastName; private String address; ... } Any Suggestions? Just generate the output Map out of the key and value of

Java split stream by predicate into stream of streams

北慕城南 提交于 2019-12-05 08:30:02
I have hundreds of large (6GB) gziped log files that I'm reading using GZIPInputStream s that I wish to parse. Suppose each one has the format: Start of log entry 1 ...some log details ...some log details ...some log details Start of log entry 2 ...some log details ...some log details ...some log details Start of log entry 3 ...some log details ...some log details ...some log details I'm streaming the gziped file contents line by line through BufferedReader.lines() . The stream looks like: [ "Start of log entry 1", " ...some log details", " ...some log details", " ...some log details", "Start

How to filter the age while grouping in map with list

别等时光非礼了梦想. 提交于 2019-12-05 08:01:24
Similar to my previous question here , the User objects I have are these new User("ayush","admin",23) new User("ashish","guest",19) new User("ashish","admin",20) new User("garima","guest",29) new User("garima","super",45) new User("garima","guest",19) Now I am trying to get the name to varying ages trend for these users. But I need to filter them above a threshold age. I could get the trend using Map<String, List<Integer>> userNameAndAgeTrend = users.stream().collect(Collectors.groupingBy(user-> user.getName(), Collectors.mapping(u-> u.getAge(), toList()))); this gives me {ashish=[19, 20],

Convert `BufferedReader` to `Stream<String>` in a parallel way

寵の児 提交于 2019-12-05 07:58:47
Is there a way to receive a Stream<String> stream out of a BufferedReader reader such that each string in stream represents one line of reader with the additional condition that stream is provided directly (before reader read everything)? I want to process the data of stream parallel to getting them from reader to save time. Edit: I want to process the data parallel to reading. I don't want to process different lines parallel. They should be processed in order. Let's make an example on how I want to save time. Let's say our reader will present 100 lines to us. It takes 2 ms to read one line

Why System.out::println is slower than anonymous class implementation in Java 8?

浪子不回头ぞ 提交于 2019-12-05 07:48:28
问题 I was working with some Java 8 Stream APIs. I am confused to see the performance difference between below two solutions, that are just printing the contents of Stream . Solution 1: int[] array = new int[] { 0, 1, 2, 3, 4, 5 }; start = System.nanoTime(); Arrays.stream(array).forEach(System.out::println); System.out.println((System.nanoTime() - start) / 1000000.0f); Solution 2: int[] array = new int[] { 0, 1, 2, 3, 4, 5 }; start = System.nanoTime(); Arrays.stream(array).forEach(new IntConsumer(

How to flatten and group this HashMap, using streams? [closed]

此生再无相见时 提交于 2019-12-05 07:27:46
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 9 months ago . Given mapping of letters to numbers, I would like to return a list of Strings, where each String is a comma delimited list of the letters grouped by their associated number. For this map Map<String, Integer> map = new HashMap<String, Integer>(); map.put("A", 1); map.put("B", 2);

Parallel stream doesn't set Thread.contextClassLoader after tomcat upgrade

假装没事ソ 提交于 2019-12-05 07:16:01
After tomcat upgrade from 8.5.6 to 8.5.28 parallel stream stopped supplying Threads with contextClassLoader: Because of it Warmer::run can't load classes in it. warmers.parallelStream().forEach(Warmer::run); Do you have any ideas what Tomcat was supplying for contextClassLoaders for new Threads? ParallelStream uses ForkJoinPool in newest Tomcat. Common ForkJoin pool is problematic and could be responsible for memory leaks and for applications being able to load classes and resources from other contexts/applications (potential security leak if your tomcat is multi tenant). See this Tomcat

Java 8 extending stream<T>

回眸只為那壹抹淺笑 提交于 2019-12-05 06:59:07
I'm attempting to extend Java 8's Stream implementation. I have this interface: public interface StreamStuff<T> extends Stream<T> { Stream<T> delegate(); default Stream<T> biggerThanFour() { return delegate().filter(i -> ((Double)i > 4)); } } And in my main method: int arr [] = {1,2,3,4,5,6}; Object array [] = ((StreamStuff)Arrays .stream(arr)) .biggerThanFour() .toArray(); I'm trying to cast the Stream, to my interface StreamStuff, and use my method. Im getting the following error: Exception in thread "main" java.lang.ClassCastException: java.util.stream.IntPipeline$Head cannot be cast to

How to restrict a Stream to run sequentially, and prevent it from running in parallel?

假如想象 提交于 2019-12-05 06:45:12
I have a method that returns a stream that is generated from a custom spliterator; the spliterator is not tread safe. Since the spliterator is not tread safe, and it maintains state, I want to prevent it from running in parallel. Is there a way to prevent the returned stream from running in parallel? I have not been able to find any documentation or examples that do this. I did find a sequential() method on the BaseStream class, but that does not appear to prevent a user from then calling parallel() to get a parallel stream. Parallel stream calls trySplit() method of your spliterator to split