| Author |
Get keys on the basis of values in any Collection
|
SaurabhSri Sri
Ranch Hand
Joined: May 01, 2008
Posts: 43
|
|
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.
|
Regards
SaurabhSri (SCJP 1.5)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Do you really mean Collection? Have you got a Map there?
|
 |
SaurabhSri Sri
Ranch Hand
Joined: May 01, 2008
Posts: 43
|
|
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
Joined: Dec 20, 2006
Posts: 89
|
|
Would it be possible to have two separate collections for stop and start? Sometimes it makes sense to store elements in several collections:
|
Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
|
 |
Vikas Kapoor
Ranch Hand
Joined: Aug 16, 2007
Posts: 1374
|
|
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
Joined: Oct 30, 2001
Posts: 1970
|
|
|
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.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
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
Joined: May 01, 2008
Posts: 43
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Guido Sautter
Ranch Hand
Joined: Dec 22, 2004
Posts: 142
|
|
|
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
Joined: Dec 22, 2004
Posts: 142
|
|
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 ]
|
 |
 |
|
|
subject: Get keys on the basis of values in any Collection
|
|
|