kaviya .P
kaviya .P

Reputation: 479

Get all the values of single key from the list map java8

I have the list as follows:

List<Map<String,Object>> mapList=new ArrayList<>();
Map<String,Object> mapObject=new HashMap<String,Object>();
mapObject.put("No",1);
mapObject.put("Name","test");
mapList.add(mapObject);
Map<String,Object> mapObject1=new HashMap<String,Object>();
mapObject1.put("No",2);
mapObject1.put("Name","test");
mapList.add(mapObject1);

and so on...

Now I want to get all the values of the key "No" as a string seperated by comma as follows:

String noList="1,2,3"

Can anyone please suggest me what may best way to do it. I know we can do it by looping but instead of looping is any other ways to do it.

Upvotes: 2

Views: 7954

Answers (4)

daniu
daniu

Reputation: 14999

You can loop like this:

List<String> noList = new ArrayList<>(mapList.size());
for (Map<String,Object> m : mapList) {
    Optional.ofNullable(m.get("No")) // get value mapped to "No" or empty Optional
        .map(Object::toString)
        .ifPresent(noList::add); // if not empty, add to list
}
System.out.println(String.join(",", noList));

or internally (the officially preferred version IIRC):

List<String> noList = new ArrayList<>(mapList.size());
mapList.forEach(m -> 
    Optional.ofNullable(m.get("No")).map(Object::toString).ifPresent(noList::add));
System.out.println(String.join(",", noList));

Now that I think of it, it's shorter than the Stream version.

Upvotes: 1

Jorge.V
Jorge.V

Reputation: 1347

Answered a pretty similar question 30 minutes ago.

You are using repeated keys. This makes it look like you don't need maps, but a class with the attributes "No", "Name", etc. If you've this class you can just iterate your instances on the list and concatenating to a String.

If no matter what you want to have your maps, simply get the values of the "No" key, but note that this is a wrong practise and you should be probably using a class instead of maps:

String res = "";

for(int i = 0; i < mapList.size(); i++) {
    Map<String,Object> map = mapList.get(i);
    res.concat(map.get("No"));
    if(i != mapList.size() - 1)
        res.concat(",");
}

PS: If you are going with the bad solution practise, use the stream alternatives in the other answers if your knowledge of stream is enough to understand them.

Upvotes: 0

Michael
Michael

Reputation: 44090

Explanations inline!

mapList.stream()                       // stream over the list
    .map(m -> m.get("No"))             // try to get the key "No"
    .filter(Objects::nonNull)          // filter any null values in case it wasn't present
    .map(Object::toString)             // call toString for each object
    .collect(Collectors.joining(",")); // join the values

Upvotes: 3

Sweeper
Sweeper

Reputation: 270768

Simply map the list:

String list = mapList.stream()
    .filter(x -> x.containsKey("No")) // get only the maps that has the key
    .map(x -> x.get("No").toString()) // every map will be transformed like this
   .collect(Collectors.joining(",")); // joins all the elements with ","
System.out.println(list);

The use of HashMap<String, Object> suggests that it might be better to create a new class for this data. Have you considered this possibility before?

Upvotes: 1

Related Questions