Hi All,
I have a HashMap object that contains Strings as both Key and Value;
Some of the keys (String) start with "rhs:"
I Need to filter the entries in the map to get all the "rhs:" entries.
I found no other way then to create a new Map and conditionally insert
entries into it.
[code]
public Map<String,String> getRhs()
{
Map<String,String> ret = new HashMap<String,String>();
for(Iterator<Map.Entry<String,String>> iter =
r.entrySet().iterator();iter.hasNext(); )
{
Map.Entry mapEntry = iter.next();
if (((String)mapEntry.getKey()).indexOf("rhs:")==0)
{
ret.put((String)mapEntry.getKey(),(String)mapEntry.getValue());
}
}
}
[/code]
If there any more efficient way to do this? how bad is it? it looks
ugly.
Thanks.
Eric Sosman - 18 Oct 2006 14:08 GMT
> Hi All,
> I have a HashMap object that contains Strings as both Key and Value;
[quoted text clipped - 24 lines]
> If there any more efficient way to do this? how bad is it? it looks
> ugly.
What do you want to do with the "filtered" Map afterwards?
Can you just leave the original Map as it stands, and write a
method or two to do what you need with the rhs: entries only?
Alternatively, you could write your own Map implementation
that's backed by two ordinary Maps: one for the rhs: entries
and one for everything else. put() and get() and so on would
check the supplied key and delegate to put() and get() on the
appropriate ordinary Map, and whenever you needed to get hold
of the rhs:-only Map it would be instantly available.

Signature
Eric Sosman
esosman@acm-dot-org.invalid
Thomas Weidenfeller - 18 Oct 2006 14:23 GMT
> I have a HashMap object that contains Strings as both Key and Value;
> Some of the keys (String) start with "rhs:"
> I Need to filter the entries in the map to get all the "rhs:" entries.
> I found no other way then to create a new Map and conditionally insert
> entries into it.
A Map (more precisely the Map API) is apparently not what you want. A
TreeMap might be more useful:
TreeMap tm = ...
SortedMap result = tm.subMap("rhs:", "rhs;");
Note the difference in the two string arguments, ':' vs. ';'.
> If there any more efficient way to do this?
I have not checked if subMap() works "efficient" (which is anyhow an
arbitrary term). But it is backed by the original TreeMap, so not much
copying should happen.
> how bad is it? it looks
> ugly.
For a HashMap, or when you only have the general Map interface, your
code is more ore less all you can do.
/Thomas

Signature
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Thomas Hawtin - 18 Oct 2006 17:16 GMT
> I have a HashMap object that contains Strings as both Key and Value;
> Some of the keys (String) start with "rhs:"
> I Need to filter the entries in the map to get all the "rhs:" entries.
> I found no other way then to create a new Map and conditionally insert
> entries into it.
> public Map<String,String> getRhs()
> {
[quoted text clipped - 5 lines]
> Map.Entry mapEntry = iter.next();
> if (((String)mapEntry.getKey()).indexOf("rhs:")==0)
^^^^^^^^Why the casts?
indexOf==0 can be replaced with startsWith^^^^^^^^^^^^^^^^^^
> {
>
> ret.put((String)mapEntry.getKey(),(String)mapEntry.getValue());
> }
> }
> }
> If there any more efficient way to do this? how bad is it? it looks
> ugly.
As Thomas Weidenfeller points out, you can use a SortedSet (or
NavigableSet in 1.6). That will make general usage, say, twice as slow,
but will improve this case.
If you don't need the original map, you can use remove of the entrySet
iterator.
Using two maps would be the obvious solution. Either one map with
startsWith "rhs:" and and other map without, or have the second map
contain all entries.
Another solution would be to write a custom map, that delegates to the
original map but filters out non-"rhs:" keys (size might have a slow
implementation).
Going further out, you could have a linked map implementation the adds
"rhs:" on one end and non-"rhs:" on the other.
The best solution depends on how it is going to be used, how much effort
you want to put into it, etc.
Tom Hawtin