Imagine that I have a list of certain objects:
List<Student>
And I need to generate another list including the ids
of Students
in the above list:
List<Integer>
Avoiding using a loop, is it possible to achieve this by using apache collections or guava?
Which methods should be useful for my case?
One way to merge multiple lists is by using addAll() method of java. util. Collection class, which allows you to add the content of one List into another List. By using the addAll() method you can add contents from as many List as you want, it's the best way to combine multiple List.
Yes, but you must work with the former account object, otherwise you will override the reference when assigning a new object.
It is possible to add all elements from one Java List into another List . You do so using the List addAll() method. The resulting List is the union of the two lists.
Java 8 way of doing it:-
List<Integer> idList = students.stream().map(Student::getId).collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With