1

I have list of hash maps and I am looking for list of UserId.

Data like:

[
 {UserId=10033, name=Siegmund},
 {UserId=10034, name=Sied},
 {UserId=10035, name=mund}
]

I am trying like:

List<HashMap<String, Object>> result = 
    (List<HashMap<String, Object>>) resultMap.get("resultList");
result.forEach(mapObj -> {
    System.out.println(mapObj.get("UserId"));
});

But looking for some better solution for this. Thanks

1
  • 1
    why do you even have a List<HashMap<String, Object>> instead of Map<UserId, String>? Commented Jan 22, 2020 at 6:39

2 Answers 2

1

You can pull all name values into a single list using something like this:

List<HashMap<String, Object>> list = ...;
List<Integer> userIds = list.stream()
                        .map(map -> (Integer) map.get("UserId"))
                        .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

0

From your data , you can have Map , why list of Maps. You can try something like below

Map<String,String> listMap = new HashMap<>();
        listMap.put("10033","Siegmund");
        listMap.put("10034","Sied");
        listMap.put("10035","mund");

        List<String> userIDList = listMap.entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());

        userIDList.stream().forEach(System.out::println);

or if you really need to parse through list of Maps then you can do as below

List<String> userIDList = listOfMaps.stream().map(Map::entrySet).flatMap(Collection::stream).map(Map.Entry::getKey).collect(Collectors.toList());

        userIDList.stream().forEach(System.out::println);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.