java-stream

Grouping a class using stream java 8 or in SQL whichever is best

一个人想着一个人 提交于 2020-01-16 09:01:31
问题 The data is something like this head child assigned total [ Mas Mas1 2 5 , Mas Mas2 0 5 , Usr usr1 4 4 , Usr usr2 1 3 , Inv Inv1 3 5 , Inv Inv2 2 3 , Inv Inv3 2 3 , Inv Inv4 1 3 ] I want sum of their child for a particular header. I am using partition in sql select head, SUM(childAssigned) over (partition by am.acl_group) as assignedHead, sum(childTotal) over (partition by am.acl_group) as totalHead, child, childAssigned, childTotal Since, "partition by" is not supported by HQL

Pattern matching in Thousands of files

有些话、适合烂在心里 提交于 2020-01-16 00:49:11
问题 I've a regex pattern of words like welcome1|welcome2|changeme ... which I need to search for in thousands of files (varies between 100 to 8000) ranging from 1KB to 24 MB each, in size. I would like to know if there's a faster way of pattern matching than doing what I have been trying. Environment: jdk 1.8 Windows 10 Unix4j Library Here's what I tried till now try (Stream<Path> stream = Files.walk(Paths.get(FILES_DIRECTORY)) .filter(FilePredicates.isFileAndNotDirectory())) { List<String>

How to use Java regex or Java streams for the given string pattern and create a map out of it

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-15 11:04:47
问题 I have a string with the following pattern, String test = "name=ravi,age=30,id=1;name=teja,age=32,id=2"; As you can see above, ";" is used to separate persons and "," is used to separate person's attributes and "=" to map the values of those attributes. I would like to convert the above string into a Map<String, Map<String, String>> where the key for the outer map is the id and the key for the inner map is the name. Given that it is always guaranteed to have unique ids and names across the

How to use Java regex or Java streams for the given string pattern and create a map out of it

会有一股神秘感。 提交于 2020-01-15 11:04:00
问题 I have a string with the following pattern, String test = "name=ravi,age=30,id=1;name=teja,age=32,id=2"; As you can see above, ";" is used to separate persons and "," is used to separate person's attributes and "=" to map the values of those attributes. I would like to convert the above string into a Map<String, Map<String, String>> where the key for the outer map is the id and the key for the inner map is the name. Given that it is always guaranteed to have unique ids and names across the

How to convert csv to a map using Java 8 Stream

风格不统一 提交于 2020-01-13 19:29:49
问题 I'm trying to create a simple parsing util that converts a two-column CSV file and put it into a map. public Map<String, String> getMapFromCSV(final String filePath) throws IOException { return Files.lines(Paths.get(filePath)) .map(line -> line.split(",")) .collect(Collectors.toMap(line -> line[0], line -> line[1])); } As you can see, I'm creating a stream of strings, delimiting each line on a comma and transforming it into a string array, and finally mapping key to index 0 and value to index

Create a sorted Set while using streams

懵懂的女人 提交于 2020-01-13 10:33:31
问题 I have a User class with name, type and age and then a long list of these users are my input List<User> users = db.getUsers(); . I am trying to create a set of all unique users from this, but the problem is I am looking for sorting them based on the age also. I have currently used- Set<User> set = users.stream().collect(Collectors.toSet()); How to sort this set at the same time as well, any idea? 回答1: It doesn't make sense to speak of order within a non sorted set. You should be using

filter Map in Java 8 Streams

泄露秘密 提交于 2020-01-13 10:11:26
问题 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;

How to divide 1 completablefuture to many completablefuture in stream?

喜你入骨 提交于 2020-01-13 10:09:24
问题 For example I have such methods: public CompletableFuture<Page> getPage(int i) { ... } public CompletableFuture<Document> getDocument(int i) { ... } public CompletableFuture<Void> parseLinks(Document doc) { ... } And my flow: List<CompletableFuture> list = IntStream .range(0, 10) .mapToObj(i -> getPage(i)) // I want method like this: .thenApplyAndSplit(CompletableFuture<Page> page -> { List<CompletableFuture<Document>> docs = page.getDocsId() .stream() .map(i -> getDocument(i)) .collect

Java 8 stream - merge collections of objects sharing the same Id

回眸只為那壹抹淺笑 提交于 2020-01-13 08:09:12
问题 I have a collection of invoices : class Invoice { int month; BigDecimal amount } I'd like to merge these invoices, so I get one invoice per month, and the amount is the sum of the invoices amount for this month. For example: invoice 1 : {month:1,amount:1000} invoice 2 : {month:1,amount:300} invoice 3 : {month:2,amount:2000} Output: invoice 1 : {month:1,amount:1300} invoice 2 : {month:2,amount:2000} How can I do this with java 8 streams? EDIT : as my Invoice class was mutable and it was not a

Java 8 stream - merge collections of objects sharing the same Id

丶灬走出姿态 提交于 2020-01-13 08:08:15
问题 I have a collection of invoices : class Invoice { int month; BigDecimal amount } I'd like to merge these invoices, so I get one invoice per month, and the amount is the sum of the invoices amount for this month. For example: invoice 1 : {month:1,amount:1000} invoice 2 : {month:1,amount:300} invoice 3 : {month:2,amount:2000} Output: invoice 1 : {month:1,amount:1300} invoice 2 : {month:2,amount:2000} How can I do this with java 8 streams? EDIT : as my Invoice class was mutable and it was not a