1

Lets say I have an ArrayList of a object User so ArrayList<user>. A User object has a property userID.

Rather than iterating the list myself and adding userIDs to a separate list, is there and API call where I could pass it the property I want on that object and have a list of these properties returned to me? Had a look through the API and nothing stood out.

Looking for a solution in Java 7 or <.

0

3 Answers 3

5

You can do this using lambdas expressions (Java 8) :

import java.util.*; import java.util.function.*; import java.util.stream.*; public class Test { public static void main(String args[]){ List<User> users = Arrays.asList(new User(1,"Alice"), new User(2,"Bob"), new User(3,"Charlie"), new User(4,"Dave")); List<Long> listUsersId = users.stream() .map(u -> u.id) .collect(Collectors.toList()); System.out.println(listUsersId); } } class User { public long id; public String name; public User(long id, String name){ this.id = id; this.name = name; } } 

Output :

[1, 2, 3, 4] 

Snippet here.


Ugliest solution using reflection :

public class Test { public static void main (String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ List<User> users = Arrays.asList(new User(1,"Alice"), new User(2,"Bob"), new User(3,"Charlie"), new User(4,"Dave")); List<Object> list = get(users,"id"); System.out.println(list); } public static List<Object> get(List<User> l, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ Field field = User.class.getDeclaredField(fieldName); field.setAccessible(true); List<Object> list = new ArrayList<>(); for(User u : l){ list.add(field.get(u)); } field.setAccessible(false); return list; } } class User { private long id; private String name; public User(long id, String name){ this.id = id; this.name = name; } } 

Output :

[1, 2, 3, 4] 
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, unfortunately we aren't running 8, but good to know this is coming.Any solution for Java 7 or <
@Michael No. But do you want to build a method that can return the list of one field ? Something like List<Object> get(List<User> l, String fieldName) ?
@Michael I added a solution using reflection. You can get the list of the property you want for all your users, you just have to provide the correct name of the field you want to collect datas. Note that I'm not very fan of this solution.
1

You can use Guava's Collections2#transform method to have the same result.

List<Integer> userIDs = Collections2.transform(users, new Function<User, Integer> (){ public Integer apply(User user) { return user.getUserID(); } }); 

Guava supports Java 7 and lower, so if you want to use an external library, the above will work in your case.

Unfortunately, you will have to do similar logic for any other objects and their inner fields you might have. It's not as a generic solution as the reflection one, it's just a more compact one.

Comments

0

It sounds like you want to use a Map.

Maps use a Key, Value pair. You can assign the userID as the key and the actual user object as the value.

You can read more here

3 Comments

True, only issue is the object is being mapped from json using Jackson, so was hoping to keep the code a bit simpler.
Simpler than a hash map? I'm not really sure what you are expecting then. That's dead simple to implement.
I think you are missing the point. Jackson parses into a list very easily. To parse into a Map it adds complexity to the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.