• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterate through Multimap by compare  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've a contents in multimap like this,



Now, I want to display it like this,

Name = Amit
Mail_Id=amit@gmail.com
Gender=male
Age=14

Name = Akash
Mail_Id=akash@gmail.com
Gender=male
Age=15

Name = Amulya
Mail_Id= amulya@gmail.com
Gender=Female
Age=16

Name = Aparna
Mail_Id=aparna@gmail.com
Gender=Female
Age=17

Name = Angle
Mail_Id=angle@gmail.com
Gender=Female
Age=18

I mean, Each first element matches to first element of the next key. How to do this?


Thanks:
Ramakrishna K.C
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Map(s) have a key and a value.

If I'm looking properly, you haven't specified whether that thing in bracket is an entry or a value.

What is the key and what is the value?


Ok, so I'm guessing that you have 4 entries in that multimap.

First key = Name
Second key = Mail_Id
Third key = Gender
Fourth key = Age

Would that be right?

While posting questions like this, wouldn't you like to post the Map declaration statement also?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the value a String[] type or an Object[] type or is it something else ( toString can print values like an Array )?
Is the key a String type or an Object type?

Edit :

I mean, Each first element matches to first element of the next key. How to do this?


There are more than one way of doing that, but what have you tried?

Hint : Arrays have indices that can be used for solving this problem. List(s) can also be accessed by indices. Arrays, Lists, and Sets can be converted to each other.



 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chan Ag

Multimap<String, String> multi = LinkedListMultimap.create();
From DB i'm retrieving data like I already shown.

First key = First_Name
Second key = First_Mail_Id
Third key = First_Gender
Fourth key = First_Age

First key = Second_Name
Second key = Second_Mail_Id
Third key = Second_Gender
Fourth key = Second_Age

like this...

I think, you got now...

Thanks:
Ramakrishna K.C
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That doesn't look like plain Java. Are you using Guava library or is that a custom implementation.

I'm not sure how Multimap of Guava library works. I haven't tried it. I can help if I have the rest of your code. Otherwise I shall pass.

I was thinking it was Map<String, <something>>. Granted you can have a Map<String,String>, and the can values look like so.



But in that case it wouldn't be a multimap.

Regardless, if it is a Map<String, String>, you can write code to remove the '[' and ']' from the value. Then you can split the value depending on a pattern you see in the value.

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its plain java and also I used "Guava", din't custom anything. I created MultiMap like what I posted and Inserting values as usual. Like you know,



In for loop. Its a simple operation in java. But, I've to loop over that map like I mentioned above.

Thanks:
Ramakrishna K.C
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so the disclaimer goes first : I haven't tried this API.

I just checked the get method. It returns you a Collection<T>. In your case, it'll return you Collection<String>.



should, for example, give you this.
[Amit, Akash, Amulya, Aparna, Angle]

Once you have a Collection<String>, you have numerous ways to get the values in any order you want. If you like, you can use the hint I suggested in my first response.

Try it. I think you might get it working along the way.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!