java-stream

Files.newDirectoryStream vs. Files.list

丶灬走出姿态 提交于 2019-12-09 16:40:49
问题 I am aware that Files.list(Path) uses Files.newDirectoryStream(Path) internally and basically just wraps the DirectoryStream. However I don't understand, when I want to use the first or the latter one. Is this just a convenience method, if I want to use the streaming API? I could have done this fairly easy myself, see this question. If one looks at the implementation of Files.list , exceptions thrown by the internal DirectoryStream are wrapped in UncheckedIOException . Anything I should know

Get object with max frequency from Java 8 stream

£可爱£侵袭症+ 提交于 2019-12-09 16:30:54
问题 I have an object with city and zip fields, let's call it Record . public class Record() { private String zip; private String city; //getters and setters } Now, I have a collection of these objects, and I group them by zip using the following code: final Collection<Record> records; //populated collection of records final Map<String, List<Record>> recordsByZip = records.stream() .collect(Collectors.groupingBy(Record::getZip)); So, now I have a map where the key is the zip and the value is a

Java 8 stream.collect( … groupingBy ( … mapping( … reducing ))) reducing BinaryOperator-usage

两盒软妹~` 提交于 2019-12-09 16:23:54
问题 I played around with a solution using groupingBy , mapping and reducing to the following question: Elegantly create map with object fields as key/value from object stream in Java 8. Summarized the goal was to get a map with age as key and the hobbies of a person as a Set . One of the solutions I came up with (not nice, but that's not the point) had a strange behaviour. With the following list as input: List<Person> personList = Arrays.asList( new Person(/* name */ "A", /* age */ 23, /*

How to check if Collection is not empty using java Stream

…衆ロ難τιáo~ 提交于 2019-12-09 16:09:27
问题 I am new to Java 8. I am not able to understand what is wrong in the following piece of code. The idea is to sent Collection<User> if its not empty. But if the collection is empty than sent HttpStatus.NOT_FOUND Entity response. @RequestMapping(value = "/find/pks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Collection<User>> getUsers(@RequestBody final Collection<String> pks) { return StreamSupport.stream(userRepository.findAll(pks)

Lambda performance improvement, Java 8 vs 11

半城伤御伤魂 提交于 2019-12-09 15:44:41
问题 I ran some JMH-tests on lambda vs method reference, looking similar to: IntStream......reduce(Integer::max) vs. IntSream.......reduce((i1, i2) -> Integer.max(i1, i2)) What I noticed was that the method reference performed about 5 times as fast as compared to the lambda, in Java 8. When i ran the test in Java 11 the execution time of the both approaches were about as fast as the method reference was in Java 8. So no major difference in performance between lambda and method reference in Java 11

Detect duplicated groups in stream

醉酒当歌 提交于 2019-12-09 15:18:28
问题 I want to ensure that all numbers in the list are grouped together. Let me explain this on examples: {1, 1, 1, 2, 2} // OK, two distinct groups {1, 1, 2, 2, 1, 1} // Bad, two groups with "1" {1, 2, 3, 4} // OK, 4 distinct groups of size 1 {1, 1, 1, 1} // OK, 1 group {3, 4, 3} // Bad, two groups with "3" {99, -99, 99} // Bad, two groups with "99" {} // OK, no groups Here's how I obtain the stream: IntStream.of(numbers) ... Now I need to pass or return true for "OK" examples and throw

IntStream of chars to Strings - Java

本秂侑毒 提交于 2019-12-09 13:59:53
问题 Is it possible to convert stream of chars str.chars() to stream with Strings, where each String contains 5 characters, for example? 回答1: I don't think trying to combine elements from the characters is a good fit for Java streams without using some third party libraries. If you want a stream of 5 character substrings I would split them like this: String s = "1234567890123456789012345678901234567890"; IntStream.range(0, s.length()/5) .mapToObj(i -> s.substring(i*5, (i+1)*5)) .forEach(System.out

In which cases Stream operations should be stateful?

本秂侑毒 提交于 2019-12-09 13:36:35
问题 In the javaodoc for the stream package, at the end of the section Parallelism , I read: Most stream operations accept parameters that describe user-specified behavior, which are often lambda expressions. To preserve correct behavior, these behavioral parameters must be non-interfering, and in most cases must be stateless . I have hard time understanding this " in most cases ". In which cases is it acceptable/desirable to have a stateful stream operation? I mean, I know it is possible,

Java 8, Lambda: Sorting within grouped Lists and merging all groups to a list

房东的猫 提交于 2019-12-09 11:54:24
问题 Based on the following answer: https://stackoverflow.com/a/30202075/8760211 How to sort each group by stud_id and then return a List with all Students as result of the grouping by stud_location and then sorting by stud_id)? It would be great to have this as extension to the existing Lambda Expression: Map<String, List<Student>> studlistGrouped = studlist.stream().collect(Collectors.groupingBy(w -> w.stud_location)); I need the Grouping based on the order of the Elements in the origin List.

java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<>

余生颓废 提交于 2019-12-09 11:49:58
问题 i try to get a list from a stream but i have an exception. Here is the Movie object with a list of an object. public class Movie { private String example; private List<MovieTrans> movieTranses; public Movie(String example, List<MovieTrans> movieTranses){ this.example = example; this.movieTranses = movieTranses; } getter and setter Here is the MovieTrans: public class MovieTrans { public String text; public MovieTrans(String text){ this.text = text; } getter and setter i add the element in the