• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Retrieve the key from List of Maps?  RSS feed

 
Greta Pereira
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a List of Maps , List<Map<String, Object>>,
say for eg, listmap prints out as - {TYPE=HTML, DISPLAY_NAME = fetch,ID=1}
I want to get the key "TYPE" from the List of Map.
How will I get the same?

If I use listmap.get("TYPE") then I get error like -
The method get(int) in the type List<Map<String, Object>> is not applicable for the arguments (String)
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greta, welcome to the Ranch!

Your requirements are a little unclear, so let me restate them and see if I have them right.

You have a List of Map<String,Object> objects. So you might have 10 different Maps if there were 10 entries in the List. And you believe that some entry in that List contains a Map where "TYPE" is a key. Is that right?

If so then you're missing out the step where you pick out that entry. You didn't say how that was going to happen so it's hard to suggest how you should proceed.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me like you only need a Map though, not a List of Maps.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Seems to me like you only need a Map though, not a List of Maps.

I'm guessing that zero or more of Greta's maps contained "TYPE". That could be handled with a single Map<String,List<Object>> but that may introduce other difficulties. If, on the other hand, there is zero or one maps containing "TYPE", then you're probably right about only needing one Map.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the input cannot be changed, you get do it quite easily using streams:

[edit] fixed Objects::notNull -> Object::nonNull[/edit]
 
Tim Holloway
Saloon Keeper
Posts: 18792
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's decompose this. You have a List of Maps.

A Map is a collection of name/value pairs where every name is unique. Substitute the word "key" for name, since actually the "name" can be any non-primitive object type, although strings are the most popular.

So, a Map Interface defines a method named keySet(), which returns - surprise! a Set of all the keys in the map. So if you have a key whose value is the String "TYPE", that will be one of the members of the returned keySet. Don't confuse the value of the key with its associated map value - which in your example was "HTML".

But you have a List of Maps. At this point things are unclear. Depending on what you're supposed to be doing, you might have the same key (TYPE) in multiple Maps, since the uniqueness of a standard Map is only within that Map, and doesn't count any other maps. So you might be looking at one of 3 possibilities:

1. The key is expected to appear in one and only one of the Maps in the List. Depending on how stringent you wish you be, you can scan the list and stop once you've found it or continue scanning and complain (throw an Exception or something) if it's also in some other map in the list

2. The Maps represent a set of overlays. For example, program options are often property maps where one layer is a config file, one is options parsed from the command line and one is hard-coded defaults. In this case, the first key found and its map "wins" the search, meaning that the value in that Map overrides any value mappings downstream of it.

3. The Maps represent a set of possibilities. In that particular case, you'd return ALL maps that contained the key in question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!