英文:
How to convert two lists into list of objects
问题
我有一个类
public class Person{
private String name;
private Integer age;
}
我还有两个列表:
List<String> names = List.of("Mike", "Piter", "Jack");
List<Integer> ages = List.of(18, 29, 30, 32);
如何从这两个列表中获取对象列表并使用流式 API?
List<Person> = ... // [{"name": "Mike", "age": 18}, {"name": "Piter", "age": 29}, {"name": "Jack", "age": 30}, {"name": null, "age": 32}]
英文:
I have a class
public class Person{
private String name;
private Integer age;
}
And i also have two lists:
List<String> names = List.of("Mike", "Piter", "Jack");
List<int> ages = List.of(18, 29, 30, 32);
How can i get the list of objects from this two lists and stream api?
List<Person> = ... // [{"name" :"Mike", "age": 18}, {"name" :"Piter", "age": 29},{"name" :"Jack", "age": 30}, {"name" :null, "age": 32}]
答案1
得分: 1
首先,List<int> ages
应该改为 List<Integer> ages
。您可以使用 IntStream.range
方法:
IntStream.range(0, Math.max(names.size(), ages.size()))
.mapToObj(i -> new Person((i < names.size() ? names.get(i): null),
(i < ages.size() ? ages.get(i): null)))
.collect(Collectors.toList());
英文:
First of all List<int> ages
should be List<Integer> ages
. You can use IntStream.range
IntStream.range(0, Math.max(names.size(), ages.size()))
.mapToObj(i -> new Person((i < names.size() ? names.get(i): null),
(i < ages.size() ? ages.get(i): null)))
.collect(Collectors.toList());
答案2
得分: 0
如果您希望在年龄为低尺寸时也将其赋值为null,则使用int数据类型无法实现。
解决方案1:
如果值不存在,则将姓名或年龄都赋值为null。
您需要将年龄的类型更改为String。
List<String> names = List.of("Mike", "Piter", "Jack");
List<String> ages = List.of("18", "29");
List<Person> collect = null;
if (names.size() > ages.size()) {
collect = IntStream.range(0, names.size())
.mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : null)))
.collect(Collectors.toList());
} else {
collect = IntStream.range(0, ages.size())
.mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
.collect(Collectors.toList());
}
System.out.println(collect);
解决方案2:
如果值不存在,则将姓名赋值为null,将年龄赋值为-1。
List<String> names = List.of("Mike", "Piter", "Jack");
List<Integer> ages = List.of(18, 29);
List<Person> collect = null;
if (names.size() > ages.size()) {
collect = IntStream.range(0, names.size())
.mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : -1)))
.collect(Collectors.toList());
} else {
collect = IntStream.range(0, ages.size())
.mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
.collect(Collectors.toList());
}
System.out.println(collect);
英文:
If you want to assign null to the age also when it has low size then it can't achieve with int data type.
Solution 1:
assign null to both name or age if the value is not there
you need to change the type of age to String
List<String> names = List.of("Mike", "Piter", "Jack");
List<String> ages = List.of("18", "29");
List<Person> collect = null;
if (names.size() > ages.size()) {
collect = IntStream.range(0, names.size())
.mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : null)))
.collect(Collectors.toList());
} else {
collect = IntStream.range(0, ages.size())
.mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
.collect(Collectors.toList());
}
System.out.println(collect);
Solution 2:
assign null to name and -1 to age if the value is not there
List<String> names = List.of("Mike", "Piter", "Jack");
List<Integer> ages = List.of(18, 29);
List<Person> collect = null;
if (names.size() > ages.size()) {
collect = IntStream.range(0, names.size())
.mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : -1)))
.collect(Collectors.toList());
} else {
collect = IntStream.range(0, ages.size())
.mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
.collect(Collectors.toList());
}
System.out.println(collect);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论