• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a map --Complicated  RSS feed

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please suggest me a solution:
i have a map map(key,value)
Map<String, MessageDTO> exampleMap = new HashMap<String, MessageDTO>();
MessageDTO msgDTO = new MessageDTO();
msgDTO.setCountOnSender(3);
msgDTO.setSenderName("hELLO");
exampleMap.put("h|110", msgDTO);
msgDTO.setCountOnSender(2);
msgDTO.setSenderName("Jeetu");
exampleMap.put("j|110", msgDTO);

Now if i want to pick those value where key is like '110' .is it possible to pick in this way.
Please suggest or tell me if i am not clear in explaining.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no such Map implementation in the core libraries, but it should be easy to extend an existing one with a get method that accepts wildcards like "*|110", and then returns a Collection of matching values.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a map with keys that look like "h|110" or "j|110" and now you want to get the values for all keys that have "110" in them? Or that end with "110"?

To do this, you need to iterate through the map and check for each key if it conforms to your search criterium. There is no easy, one method call solution for this. Something like this:

[ September 21, 2007: Message edited by: Jesper Young ]
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it is not very clear to me what the title "Sorting..." has to do with the actual question?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just guessing, but do you really have two different keys? For example, the "j" bit and the "110" bit.

If that's the case, then you should not use the compound key (e.g. "j|110"), but instead should put two separate entries in the Map, one with each key. That's perfectly legal and allows you to look up by either "j" or "110". If you also want to be able to look up combinations of the keys, then also put entries in the Map for the compound key.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper . I will try this.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tweety Samuel:
Thanks Jesper . I will try this.


Bear in mind that Jesper's solution is just an iteration through the whole map. It does not utilise the hashing of the map at all, and hence will not scale well.

This is not a criticism of Jesper's solution, as it is the only way to do it, if you are required to store the keys like you initially say (e.g. "j|110").

However, if you can split up the compound keys like I suggest and store multiple map entries per compound key, then you will still be utilising the hashing of the map, and hence your code will scale much better.

You haven't answered whether the splitting of the compound keys is feasible in your situation.
[ September 21, 2007: Message edited by: Peter Chase ]
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO, its not feasible in my case.

Thanks to all!!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, what Peter said.
 
Huan Niu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my solution:


Hopefully, this is what you want.

By the way, in this example, you will get two identical values...
[ September 24, 2007: Message edited by: Huan Niu ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!