Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Binary Search
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Tom Anderson, post: 4199783"] Yes, and i tried not to think about it, because it's smelly. How do you obtain these objects? Do you make them purely to use as key-holders? Does that mean you're going to have half-filled-out instances of your value class floating about the place? Does that mean you have to provide a constructor that allows such instances to be created, so abandoning the invariants that you could otherwise enforce through your constructors? Or do you need to do something like: public interface KeyHolder { public String getKey(); } public class Value implements KeyHolder { private final String key; // other fields public String getKey() { return key; } // other methods } public class Example implements KeyHolder { // so called for "query by example" private final String key; public Example(String key) { this.key = key; } public String getKey() { return key; } } Map<KeyHolder, Value> objects = new TreeMap<KeyHolder, Value>(new Comparator<KeyHolder, KeyHolder>(){ public int compare(KeyHolder a, KeyHolder b) { return a.getKey().compareTo(b.getKey()); } }); Value v = ...; String k = v.getKey(); objects.put(v, v); Value u = objects.get(new Example(k)); assert u == v; ? It can certainly be done, but all things considered, i lean towards extracting the key before hitting the map. The map could perhaps be wrapped in a simple wrapper: public class ValueStore { private Map<String, Value> map = new HashMap<String, Value>(); public void put(Value v) { map.put(v.getKey(), v); } public Value get(String key) { return map.get(key); } } So that calling code is not troubled with the details. tom [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Binary Search
Top