Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Concurrent bidirectional one-to-many map? Date: Fri, 06 May 2011 13:45:53 -0700 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 6 May 2011 20:46:05 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="l8hxu7VxWTF0R70MQnZDgg"; logging-data="17791"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZkCn0ouLwIs7AKcBYCKpgb0x5mdgHcD0=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:T+ihHD1B4cLn1vMTtoZd3Tjn0+8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3713 On 5/6/2011 1:07 PM, Sebastian wrote: > Does anyone know of a concurrent bidirectional one-to-many > map implementation? > > By bidirectional I mean that I can lookup keys by values, by > one-to-many I mean that the value end of the map is a list or > set, and by concurrent I mean that I do not need to synchronize > externally and get performance comparable to that of > java.util.concurrent.ConcurrentHashMap in both directions. > > If this beast doesn't exist, how would I go about inventing it? > I must say that I am by no means any sort of concurrency guru... > > -- Sebastian > Can you just put both keys and values in as the key? Something like: Map myMap = new ConcurrentHashMap(); // add key k and value v Key holder = new Key( k, v, KEY ); myMap.put( holder, holder ); holder = new Key( v, k, VALUE ) myMap.put( holder, holder ); ... public class Key { public enum KeyValue {KEY,VALUE}; private k key; private V value; private KeyValue keyOrValue; public Key( K key, V value, KeyValue keyOrValue ) { this.key = key; this.value = value; this.keyOrValue = keyOrValue; } // must also override hashcode and equals... } This way when you add something, it gets added as both key and value. You might want to override "put" to do that automatically. When you look up either a key or a value, you'll find both. There may be better ways of doing this, it's just the first thing that I thought of. Also the code above is meant more as a sketch than a rigorous code example.