-1

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}]
Pikachu
  • 309
  • 4
  • 14
  • Hi, have you even tried, at least, to search for a similar questions/problems? I am certain there would've been quite a few useful answers/articles/pages should you have attempted to look for them. – Giorgi Tsiklauri Oct 13 '20 at 11:43

2 Answers2

1

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());
Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • This is what I need, but what if the names size is less than age size? Then there will be an exception. Is it possible to somehow set null if the names run out – Pikachu Oct 13 '20 at 10:33
  • Don't know the purpose, but below thing should work for you IntStream.range(0, ages.size()) .mapToObj(i -> new Person(i < names.size() ? names.get(i) : null, ages.get(i))) .collect(Collectors.toList()); – Nagaraja JB Oct 13 '20 at 10:39
  • What is the desired output if the names size is greater than age size ? – Eklavya Oct 13 '20 at 10:43
  • Set null in the field if the list size is greater than another list size. For example, if the names size is greater than age size - it is necessary to set null in the 'age' field. And if the ages size is greater than names size - it is necessary to set null in the 'name' field. – Pikachu Oct 13 '20 at 11:11
0

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);