Searching in a TreeMap (Java)
Posted
by Kronen
on Stack Overflow
See other posts from Stack Overflow
or by Kronen
Published on 2010-06-01T22:47:36Z
Indexed on
2010/06/01
22:53 UTC
Read the original article
Hit count: 156
I need to do a search in a map of maps and return the keys this element belong. I think this implementation is very slow, can you help me to optimize it?. I need to use TreeSet and I can't use contains because they use compareTo, and equals/compareTo pair are implemented in an incompatible way and I can't change that. (sorry my bad english)
Map>> m = new TreeSet();
public String getKeys(Element element) { for(Entry>> e : m.entrySet()) { mapSubKey = e.getValue(); for(Entry> e2 : mapSubKey.entrySet()) { setElements = e2.getValue(); for(Element elem : setElements) if(elem.equals(element)) return "Key: " + e.getKey() + " SubKey: " + e2.getKey(); } } }
© Stack Overflow or respective owner