• Post Reply Bookmark Topic Watch Topic
  • New Topic

loop through arraylist in the Multimap  RSS feed

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I have a Multimap which stores string,integer and list as a value to the keys. Below is code that I tried. But the problem is I want to get the values for the list too. I have showed the output that I am getting and the output that I am looking for.





But the output that I want is


How do I go about doing this? Is there a way to know if valuesIterator.next( ) is a list? so that I can loop through it if it is a list or just print the values if its just the string or integer value?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you define values a Collection, so I would say that it is definitely a list object.

Hunter
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I see it is a list object but how do i extract values from the list. The below code will not work since valuesIterator.next() is not an array. When looped through it displays the string, integer and finally the memory location of the list. But how do I get the values of the list?



Any hints please...
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with that MultiMap is that it actually violates the contract for Map, and can never be changed to use generics -- the put method takes a value type whereas get and remove return collections of that value type.

Anyway, the approach to use would probably be to use entrySet():
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using entrySet didn't work.



This is the result I got



The problem is the last value to the map is an array inside a list. So the actual problem is how do I extract the value from the aray inside a list. Any suggestion please or is using any other maps a better option. The criteria is the key should accept more than one values and there two or more different keys. The values are of different datatypes such as string,integer and an array inside a list.
Any ideas please
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all you need to do is do an instanceof Collectoion while doing:


if its an instance of collection or more specifically, an array list, cast it, and iterate it again...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!