You can write such a function with Stream
s:
public static List<String> getPropertyList(List<Map<String,String>> users, String key)
{
return users.stream()
.map(m -> m.get(key))
.collect(Collectors.toList());
}
Then call it with:
List<String> userIds = getPropertyList(users,"id");
List<String> userNames = getPropertyList(users,"name");
...
P.S. I changed the type of the list from ArrayList<HashMap<String,String>>
to List<Map<String,String>>
, since it's better to program to interfaces.
If you are using Java 7 or older, you'll need a loop:
public static List<String> getPropertyList(List<Map<String,String>> users, String key)
{
List<String> result = new ArrayList<String>();
for (Map<String,String> map : users) {
result.add(map.get(key));
}
return result;
}