[英]How do I combine 2 array lists of objects having same property value in java
我有 2 个数组列表,如下所示。 一个数组包含用id
、 name
和city
映射的学生信息。 另一个数组包含用id,
marks and
grade 映射的标记详细信息. The goal is to compare each of these elements against each other and combine them based on the common field id in both the
. The goal is to compare each of these elements against each other and combine them based on the common field id in both the
。
student_details=[
{
"id": 1,
"name": "sam",
"city": "Chennai"
},
{
"id": 2,
"name": "peter",
"city": "Chennai"
}
]
student_grades=[
{
"id": 1,
"marks": 95,
"grade": "A"
},
{
"id": 2,
"marks":63,
"grade": "B"
}
]
结果应如下所示。
[
{
"id": 1,
"name": "sam",
"city": "Chennai",
"marks": 95,
"grade": "A"
},
{
"id": 2,
"name": "peter",
"city": "Chennai",
"marks":63,
"grade": "B"
}
]
我使用下面的代码在 javascript 中实现了相同的功能。 如何在 java 中实现这个?
async mergeData(data1, data2, key) {
const result = data1.map(a =>
Object.assign({}, a,
data2.find(b => b[key] === a[key])
)
)
return result
}
如果您的数据包含的属性始终相同,那么最简单的方法是创建一个 ArrayList,然后在将所有条目添加到 ArrayList 后调用 toArrayMethod,否则您必须考虑 id 不存在的不同情况match 这将需要大量代码来解释不同的数组大小。
然后,您只需在给定的 arrays 上迭代 2 个嵌套 for 循环,如果 id 匹配,则使用 ArrayList 的 add 方法将新条目添加到 Z57A97A39435CFDFED96E03F2A3BC2。
The other question is what object type your data actually has, because I don't think you can create object pairs like of the nature of "attribute_name" = value
in java, so your input is probably some json you're reading in. You could use gson or other libraries to map your json to 2 java classes, then use another 3rd class for the combined data.
Another way would be to map it yourself with a json handling library to a java class or some datastructure like a HashMap. 如果您不知道 java class 将来会有什么属性,那么 HashMap 最有意义。
Another way would be to map the object from json to another json with a json handling library, with 2 for loops iterating over your json arrays.
public static void main(String... args) {
List<Map<String, String>> studentDetails = List.of(
Map.of("id", "1", "name", "sam", "city", "Chennai"),
Map.of("id", "2", "name", "peter", "city", "Chennai"));
List<Map<String, String>> studentGrades = List.of(
Map.of("id", "1", "marks", "95", "grade", "A"),
Map.of("id", "2", "marks", "63", "grade", "B"));
List<Map<String, String>> res = merge(studentDetails, studentGrades);
}
public static List<Map<String, String>> merge(
List<Map<String, String>> studentDetails,
List<Map<String, String>> studentGrades) {
Map<String, Map<String, String>> map = new HashMap<>();
Consumer<Map<String, String>> add = studentData -> {
String id = studentData.get("id");
map.computeIfAbsent(id, key -> new HashMap<>());
map.get(id).putAll(studentData);
};
studentDetails.forEach(add);
studentGrades.forEach(add);
return new ArrayList<>(map.values());
}
您可以使用如下流和记录,如下所示:
package pl.maro;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MyUtils {
public static void main(String[] args) {
var studentDetails = List.of(
new StudentDetail(1, "sam", "Chennai"),
new StudentDetail(2, "peter", "Chennai")
);
var studentGrades = List.of(
new StudentGrade(1, 95, 'A'),
new StudentGrade(2, 63, 'B')
);
List<StudentDao> studentDaos = yourMethod(studentDetails, studentGrades);
studentDaos.forEach(System.out::println);
}
private static List<StudentDao> yourMethod(List<StudentDetail> studentDetails, List<StudentGrade> studentGrades) {
var detailMap = studentDetails.stream().collect(Collectors.toMap(k -> k.id, v -> v));
var gradeMap = studentGrades.stream().collect(Collectors.toMap(k-> k.id, v->v));
return Stream.of(detailMap.keySet(), gradeMap.keySet())
.flatMap(Collection::stream)
.collect(Collectors.toSet())
.stream()
.filter(id -> detailMap.containsKey(id) && gradeMap.containsKey(id))
.map(id -> createStudentDao(detailMap.get(id), gradeMap.get(id)))
.toList();
}
private static StudentDao createStudentDao(StudentDetail studentDetail, StudentGrade studentGrade) {
return new StudentDao(studentDetail.id, studentDetail.name, studentDetail.surname, studentGrade.marks, studentGrade.grade);
}
record StudentDetail(int id, String name, String surname) {}
record StudentGrade(int id, int marks, char grade) {}
record StudentDao(int id, String name, String surname, int marks, char grade) {
@Override
public String toString() {
return "StudentDao{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", marks=" + marks +
", grade=" + grade +
'}';
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.