This site requires JavaScript, please enable it in your browser!
Greenfoot back
Newbie
Newbie wrote ...

2016/1/28

Interface similar to Map

Newbie Newbie

2016/1/28

#
For my current programme I need something similar to the Map Interface. The problem with the HashMap I am using at the moment is that you can save more than one key with the same value. For my progamme it is important that there can only be one key for the same value. Is there any Interface working this way?
Newbie Newbie

2016/1/28

#
I also just found the method remove with the key as a parameter. Is there a similar method with the value as a parameter? Is there any replace method similar to the replace method defined for tree map? This method replaces the value for a given Object, but I would like to replace the Object of any given value.
danpost danpost

2016/1/28

#
I do not think there is an interface where both keys and their values are restricted to one time use. That does not mean you cannot create one yourself by extending the HashMap class with the added restriction.
davmac davmac

2016/1/28

#
To add to what danpost has already said: the interface you describe sounds like a "Bimap", a map which maps both ways between keys and values. There is (I think) no implementation or declared interface for a Bimap in the Java standard library, though you might be able to find an implementation in another library. Or, you can produce similar functionality yourself easily enough using two HashMap instances - one mapping the keys to values, the other mapping values back to their respective keys. You need to take care that when you update one of the HashMap instances you also update the other as appropriate. (For instance if you change a key -> value mapping, you must remove the old value -> key mapping and insert a new one).
Newbie Newbie

2016/1/29

#
I found the Interface BidiMap on the Internet. It apparently extends IterableMap, which extends Map itself, so importing java.util.* should actually do the job shouldn't it? But for some reason it says it doesn't know the Class I am referring to. At the moment I got this code creating my hashmap:
    
public static Map<String,Integer> checkDouble = new HashMap<String,Integer>();
Changing the HashMap to BidiMap doesn't do the job. How would I have to do this?
Newbie Newbie

2016/1/29

#
I also found this to be the code importing BidiMap:
import org.apache.commons.collections.BidiMap;
However, The compiler says "package org.apache.commons.collections does not exist"...
Newbie Newbie

2016/1/29

#
Update: I have downloaded the apache commons collections 4.1 package. Where do I have to save it and how can I use it in my code?
danpost danpost

2016/1/29

#
There is also a 'containsValue' method of the Map class that returns a boolean value which can be used as an 'if' condition to prevent duplicate values being added to the map.
Newbie Newbie

2016/1/29

#
Yes I know, but this is just partially what I need.
Newbie Newbie

2016/2/1

#
Is there no one who could help me with this matter? :-(
danpost danpost

2016/2/1

#
By adding the following class to your project, you can create a StrictHashMap object that prevents duplicate values:
public class StrictHashMap<K, V> extends java.util.HashMap
{
    public V strictPut(K key, V value)
    {
        if (containsValue(value)) return null;
        return (V)super.put(key, value);
    }
    
    public V strictReplace(K key, V value)
    {
        if (!containsKey(key)) return null;
        return strictPut(key, value);
    }
}
Just use 'strictPut' instead of 'put' and 'strictReplace' instead of 'replace.
You need to login to post a reply.