0

in Java which is the best way to copy the objects UserAndSum from one List to one other List.

EDIT: I have a list of OBJ users with many users and I want a new one with the first 25 users only

List <UserAndSum> list= new ArrayList<UserAndSum>();
list.add(new UserAndSum(john, 55));
list.add(new UserAndSum(tom, 88));
list.add(new UserAndSum(brian, 99));
....

List <UserAndSum> list25 = new ArrayList<UserAndSum>();
???? ADD ONLY 25 UserAndSum????
user2598571
  • 37
  • 1
  • 4
  • 4
    You want to copy the objects or the object references? – Sotirios Delimanolis Feb 12 '15 at 17:01
  • I would like to coypy the obj UserAndSum from one list to the other so I can have the same list with only the first 25 users – user2598571 Feb 12 '15 at 17:02
  • Does changing state of object in one list should also affect its copy from other list? For instance if you copy `UserAndSum(john, 55)` from `list` to `list25` and you will set age to `56` in `list` should this change be also visible in `list25`? – Pshemo Feb 12 '15 at 17:04
  • sorry I try to explain me very well... I have a list of OBJ users with many users and I want a new one with the first 25 users only – user2598571 Feb 12 '15 at 17:08
  • can we do deep cloning http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents? – Kick Buttowski Feb 12 '15 at 17:11

1 Answers1

4

If you want placed in list25 first 25 elements from list (without actyally creating copies of UserAndSum) then you can simply write

List <UserAndSum> list25 = list.subList(0, Math.min(25, list.size()));

I added Math.min(25, list.size()) in case list would have less elements than 25.


You can create little clearer code using Java 8 and its stream().

List <UserAndSum> list25 = list.stream().limit(25).collect(Collectors.toList());

Both solutions will produce same result, but limit(25) IMO describes want to achieve easier than Math.min(25, list.size()).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • does deep cloning make sense here? – Kick Buttowski Feb 12 '15 at 17:13
  • 1
    @KickButtowski It depends on what OP really wants to achieve. My answer is based on assumption that we don't need it, as I tried to explain at start of my post. – Pshemo Feb 12 '15 at 17:15
  • Personally I find "sublist" to be a lot clearer of a method call (both in syntax and semantics) than "stream().limit().collect()" but that is where times are heading unfortunately. Personally I struggle to see how there are any performance gains with the new syntax either... This is not really the place to voice these issues; your code is good, but grrrr map functions. – gnomed Feb 12 '15 at 17:25
  • 2
    @gnomed "*I struggle to see how there are any performance gains with the new syntax either*" in this case there will be no real performance gain. I suspect that we will start seeing it when using `parallelStream()` which will be able to split tasks into smaller ones (probably sorting with merge-sort could be good example) and execute them simultaneously. But yes, while `limit(25)` may be more meaningful `collect` can add more confusion so this approach will be easier to read probably only to people used to it. – Pshemo Feb 12 '15 at 17:43