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

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

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&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
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?

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 方法:

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&lt;int&gt; ages should be List&lt;Integer&gt; ages. You can use IntStream.range

IntStream.range(0, Math.max(names.size(), ages.size()))
         .mapToObj(i -&gt; new Person((i &lt; names.size() ? names.get(i): null),
                                   (i &lt; 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&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
    List&lt;String&gt; ages = List.of(&quot;18&quot;, &quot;29&quot;);

    List&lt;Person&gt; collect = null;
    if (names.size() &gt; ages.size()) {
        collect = IntStream.range(0, names.size())
                .mapToObj(i -&gt; new Person(names.get(i), (i &lt; ages.size() ? ages.get(i) : null)))
                .collect(Collectors.toList());
    } else {
        collect = IntStream.range(0, ages.size())
                .mapToObj(i -&gt; new Person(i &lt; 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&lt;String&gt; names = List.of(&quot;Mike&quot;, &quot;Piter&quot;, &quot;Jack&quot;);
    List&lt;Integer&gt; ages = List.of(18, 29);

    List&lt;Person&gt; collect = null;
    if (names.size() &gt; ages.size()) {
        collect = IntStream.range(0, names.size())
                .mapToObj(i -&gt; new Person(names.get(i), (i &lt; ages.size() ? ages.get(i) : -1)))
                .collect(Collectors.toList());
    } else {
        collect = IntStream.range(0, ages.size())
                .mapToObj(i -&gt; new Person(i &lt; names.size() ? names.get(i) : null, ages.get(i)))
                .collect(Collectors.toList());
    }

    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:

确定