• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get keys on the basis of values in any Collection  RSS feed

 
SaurabhSri Sri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there any way to get keys on the basis of values in any Collection. Actually my collection looks like this -

Service1 Start
Service2 Stop
Service3 Start
Service4 Start
Service5 Stop

Now I want to get all keys whose values are 'Stop' in a sorted manner. Please give any idea or if any other way ..

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those keys? You usually only have keys if you are using a Map.

Do you mean to sort the elements? You will need to learn how to work a Comparator.
How about iterating through your Collection, finding the elements, and if they have "stop" in adding them to a new Collection. If you want them in order, use a List.
If you want them sorted, try a TreeSet and a Comparator (but read the TreeSet API carefully first; "start" and "stop" might not work in a TreeSet) OR a List and sort the List.
I presume you are familiar with the Collections Framework? Java Tutorial link here.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really mean Collection? Have you got a Map there?
 
SaurabhSri Sri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for your quick reply.

I have a HashMap where I am putting some services as keys and there status (start or stop) as values. Now, I just want to get all services which have status as start (or sometimes stop). So, basically I want to get keys on the basis of values. To solve this I have implemented following code and it is working for me but still have some problem that the values are not coming in sorted manner.

HashMap map = new HashMap();

map.put("service1","start");
map.put("service2","stop");
map.put("service3","start");
map.put("service4","start");
map.put("service5","stop");

Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext())
{
Map.Entry me =(Map.Entry) it.next();
String val = (String)me.getValue();

if(val.equalsIgnoreCase("start"))
{
System.out.println("Service Started - " + me.getKey());
}
}
Thanks
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be possible to have two separate collections for stop and start?


Sometimes it makes sense to store elements in several collections:

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SaurabhSri ,
I think you can assign some constants ( priority value)to all the services and use them as key.
And once you get list/array of keys (after comparing values("start")). You can sort them as well.and execute your services accordingly.
Could be some better solution exists.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you want a two-way map. There's none in the JDK, but you can easily make one, by combining two HashMaps. One would take the service name as key and status as value, the other would take status as key and name as value. You must make sure that you always add to both maps and remove from both maps.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you had a Map and not a Collection.
You have found the entrySet() method, and you can get all the keys and all the values out into their own Sets, Lists or whatever. Or, as you can see, you can iterate through the Set.

Peter Chase's suggestion is very good as an alternative.

You will probably find your Iterator easier to handle if you generify it.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
It sounds like you want a two-way map. There's none in the JDK, but you can easily make one, by combining two HashMaps. One would take the service name as key and status as value, the other would take status as key and name as value. You must make sure that you always add to both maps and remove from both maps.
The only problem with that approach is that one of the Maps will have only "start" and "stop" as its keys.
 
SaurabhSri Sri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Using two Hash maps doesn't make sense for me. I have taken some different approach here. Now, my Hash map is having status as key and list of services as values and I am cool with this.

Thanks a lot to all of you for your help.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
It sounds like you want a two-way map. There's none in the JDK, but you can easily make one, by combining two HashMaps.


Or simply use an existing open source implementation: http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/BidiMap.html
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the status as the key, it might be easier to just use two Lists (startList and stopList, for instance) and forget about the HashMap altogether. Basically, if you know all the possible keys beforehand, and there's only very few of them, using a Map might not be the best way.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Or simply use an existing open source implementation: http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/BidiMap.html


The documentation of the BidiMap interface says that in such a Map, no two keys must map to the same value so inversion is unambiguous. So using the BidiMap might not be the best approach in this particular case.

Do you know of some other bidirectional Map where the inversion is defined in terms of values being mapped to Sets of keys? Or maybe just a Map providing an inverse lookup method like

[ May 05, 2008: Message edited by: Guido Sautter ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!