collectors

Groupingby a list of objects based on an attribute which can be null

不羁岁月 提交于 2021-02-20 00:50:36
问题 I have a list of Student Objects as below. Student1 DOB : 12/02/2010 Student2 DOB : 12/03/2010 Student1 DOB : 12/04/2010 Student4 DOB : Student2 DOB : Student3 DOB : 12/01/2010 Student{ String name; Date dob; } I want to group the Students based on the DOB as below. All Students should be grouped based on the student name. Students must be in descending order of dob. Same Students grouped together should be in descending order of dob. Remaining Student objects must be in same order as it is

Groupingby a list of objects based on an attribute which can be null

笑着哭i 提交于 2021-02-20 00:49:14
问题 I have a list of Student Objects as below. Student1 DOB : 12/02/2010 Student2 DOB : 12/03/2010 Student1 DOB : 12/04/2010 Student4 DOB : Student2 DOB : Student3 DOB : 12/01/2010 Student{ String name; Date dob; } I want to group the Students based on the DOB as below. All Students should be grouped based on the student name. Students must be in descending order of dob. Same Students grouped together should be in descending order of dob. Remaining Student objects must be in same order as it is

How to groupBy object properties and map to another object using Java 8 Streams?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-18 09:53:09
问题 Suppose I have a group of bumper cars, which have a size, a color and an identifier ("car code") on their sides. class BumperCar { int size; String color; String carCode; } Now I need to map the bumper cars to a List of DistGroup objects, which each contains the properties size , color and a List of car codes. class DistGroup { int size; Color color; List<String> carCodes; void addCarCodes(List<String> carCodes) { this.carCodes.addAll(carCodes); } } For example, [ BumperCar(size=3, color

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

假如想象 提交于 2021-02-16 13:08:23
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

别说谁变了你拦得住时间么 提交于 2021-02-16 13:04:10
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

坚强是说给别人听的谎言 提交于 2021-02-16 13:03:10
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

groupingby list of arrays

假如想象 提交于 2021-02-11 08:10:37
问题 I get a list of object arrays that I need to group. The arrays contain different types of objects. Here is an example: List<Object[]> resultList = query.getResultList(); // call to DB // Let's say the object array contains objects of type Student and Book // and Student has a property Course. // Now I want to group this list by Student.getCourse() Map<String, Object[]> resultMap = resultList.stream().collect(Collectors.groupingBy(???)); What do I provide to the Collectors.groupingBy() method

How to groupingBy into a Map and change the key type

依然范特西╮ 提交于 2021-02-08 23:40:26
问题 I have a code which is suppose to group a list of transaction objects into 2 categories; public class Transaction { public String type; public Integer amount; } The following function divided the list into 2 categories by checking a condition. The output map of the stream operation is Map<Boolean, List<Transaction>> , but I would like to use a String as its key. So I converted them manually. public static Map<String, List<Transaction>> partitionTransactionArray(List<Transaction> t1) { Map

Java Stream API : what kind of map method collect(Collectors.toMap()) returns?

风格不统一 提交于 2021-02-08 17:35:27
问题 What kind of map "hm" is? Map<String,Person> hm; try (BufferedReader br = new BufferedReader(new FileReader("person.txt")) { hm = br.lines().map(s -> s.split(",")) .collect(Collectors.toMap(a -> a[0] , a -> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3])))); Does it depend on declaration? Map<String,Person> hm = new HashMap<>(); Map<String,Person> hm = new TreeMap<>(); 回答1: No, initializing the variable referenced by hm is pointless, since the stream pipeline creates a new

How to merge child objects in list based on duplicate parent object

久未见 提交于 2021-02-08 03:45:30
问题 I have two entities with one to many relationship which are joined together by composite primary keys. Since Spring Data generates wrong count distinct query for oracle database , I have SQL output with cartesian join which leads to repeating row for parent object for every row of child object. I need to find out distinct parent objects based on composite keys and then add each child object for parent in a list and set it as property of parent object I am able to find out distinct parent