如何将两个列表转换为对象列表

huangapple go评论106阅读模式
英文:

How to convert two lists into list of objects

问题

我有一个类

  1. public class Person{
  2. private String name;
  3. private Integer age;
  4. }

我还有两个列表:

  1. List<String> names = List.of("Mike", "Piter", "Jack");
  2. List<Integer> ages = List.of(18, 29, 30, 32);

如何从这两个列表中获取对象列表并使用流式 API?

  1. List<Person> = ... // [{"name": "Mike", "age": 18}, {"name": "Piter", "age": 29}, {"name": "Jack", "age": 30}, {"name": null, "age": 32}]
英文:

I have a class

  1. public class Person{
  2. private String name;
  3. private Integer age;
  4. }

And i also have two lists:

  1. List&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
  2. List&lt;int&gt; ages = List.of(18, 29, 30, 32);

How can i get the list of objects from this two lists and stream api?

  1. List&lt;Person&gt; = ... // [{&quot;name&quot; :&quot;Mike&quot;, &quot;age&quot;: 18}, {&quot;name&quot; :&quot;Piter&quot;, &quot;age&quot;: 29},{&quot;name&quot; :&quot;Jack&quot;, &quot;age&quot;: 30}, {&quot;name&quot; :null, &quot;age&quot;: 32}]

答案1

得分: 1

首先,List&lt;int&gt; ages 应该改为 List&lt;Integer&gt; ages。您可以使用 IntStream.range 方法:

  1. IntStream.range(0, Math.max(names.size(), ages.size()))
  2. .mapToObj(i -> new Person((i < names.size() ? names.get(i): null),
  3. (i < ages.size() ? ages.get(i): null)))
  4. .collect(Collectors.toList());
英文:

First of all List&lt;int&gt; ages should be List&lt;Integer&gt; ages. You can use IntStream.range

  1. IntStream.range(0, Math.max(names.size(), ages.size()))
  2. .mapToObj(i -&gt; new Person((i &lt; names.size() ? names.get(i): null),
  3. (i &lt; ages.size() ? ages.get(i): null)))
  4. .collect(Collectors.toList());

答案2

得分: 0

如果您希望在年龄为低尺寸时也将其赋值为null,则使用int数据类型无法实现。

解决方案1:
如果值不存在,则将姓名或年龄都赋值为null。
您需要将年龄的类型更改为String。

  1. List<String> names = List.of("Mike", "Piter", "Jack");
  2. List<String> ages = List.of("18", "29");
  3. List<Person> collect = null;
  4. if (names.size() > ages.size()) {
  5. collect = IntStream.range(0, names.size())
  6. .mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : null)))
  7. .collect(Collectors.toList());
  8. } else {
  9. collect = IntStream.range(0, ages.size())
  10. .mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
  11. .collect(Collectors.toList());
  12. }
  13. System.out.println(collect);

解决方案2:
如果值不存在,则将姓名赋值为null,将年龄赋值为-1。

  1. List<String> names = List.of("Mike", "Piter", "Jack");
  2. List<Integer> ages = List.of(18, 29);
  3. List<Person> collect = null;
  4. if (names.size() > ages.size()) {
  5. collect = IntStream.range(0, names.size())
  6. .mapToObj(i -> new Person(names.get(i), (i < ages.size() ? ages.get(i) : -1)))
  7. .collect(Collectors.toList());
  8. } else {
  9. collect = IntStream.range(0, ages.size())
  10. .mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i)))
  11. .collect(Collectors.toList());
  12. }
  13. 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

  1. List&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
  2. List&lt;String&gt; ages = List.of(&quot;18&quot;, &quot;29&quot;);
  3. List&lt;Person&gt; collect = null;
  4. if (names.size() &gt; ages.size()) {
  5. collect = IntStream.range(0, names.size())
  6. .mapToObj(i -&gt; new Person(names.get(i), (i &lt; ages.size() ? ages.get(i) : null)))
  7. .collect(Collectors.toList());
  8. } else {
  9. collect = IntStream.range(0, ages.size())
  10. .mapToObj(i -&gt; new Person(i &lt; names.size() ? names.get(i) : null, ages.get(i)))
  11. .collect(Collectors.toList());
  12. }
  13. System.out.println(collect);

Solution 2:
assign null to name and -1 to age if the value is not there

  1. List&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
  2. List&lt;Integer&gt; ages = List.of(18, 29);
  3. List&lt;Person&gt; collect = null;
  4. if (names.size() &gt; ages.size()) {
  5. collect = IntStream.range(0, names.size())
  6. .mapToObj(i -&gt; new Person(names.get(i), (i &lt; ages.size() ? ages.get(i) : -1)))
  7. .collect(Collectors.toList());
  8. } else {
  9. collect = IntStream.range(0, ages.size())
  10. .mapToObj(i -&gt; new Person(i &lt; names.size() ? names.get(i) : null, ages.get(i)))
  11. .collect(Collectors.toList());
  12. }
  13. System.out.println(collect);

huangapple
  • 本文由 发表于 2020年10月13日 18:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64333083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定