Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a Map where every Key points to the same Value?

Tags:

java

hashtable

Is it possible (In Java) to create a Map where, no matter what key I was to search for, I would retrieve the same value? We can assume either a finite or infinite amount of keys.

I considered a map of size 1 and load factor of 1.0, with that value stored in it, but I'm nearly positive that the hashmap implementation will recognize the collision, and return null anyway.

I also entertained the possibility that if I created my own hashfunction for a variable, or even a new datatype that implemented Map, that I should be able to do this, but it may be a little messy. Perhaps not?

Of course, simply mapping the value to every single key would be very inefficient (unless there is a built in method for this, that I overlooked), and nowhere near as entertaining as hearing SO's answers.

like image 700
Addison Avatar asked Mar 18 '23 00:03

Addison


2 Answers

I suppose the most reasonable way to do this with existing API is like the following:

// Java 6/7
new TreeMap<K, V>(new Comparator<K>() {
    @Override
    public int compare(K lhs, K rhs) {
        return 0;
    }
});

// Java 8+
new TreeMap<K, V>((a, b) -> 0);

This TreeMap considers all keys equal but will otherwise maintain pretty good Map semantics.

Note that the first key you put in the Map will stay in it forever unless you remove it.

i.e.

m.put("abc", "123");
m.put("def", "456");
// prints something like {abc=456}
System.out.println(m);

So you might keep that in mind for example if you planned on examining the entrySet.

like image 95
Radiodef Avatar answered May 06 '23 17:05

Radiodef


I'm not too sure on what you're trying to accomplish here but all you need to do is implement Map. An example:

import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class ImmutableMap<K, V> implements Map<K, V> {

    private final V immutableValue;

    public ImmutableMap(final V immutableValue) {
        this.immutableValue = immutableValue;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean containsKey(Object key) {
        return false;
    }

    @Override
    public boolean containsValue(Object value) {
        if (value == immutableValue)
            return true;
        return false;
    }

    @Override
    public V get(Object key) {
        return immutableValue;
    }

    @Override
    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    /**
     * Below are some unnecessary methods for the purpose of this class.
     * You may decide their action.
     */

    @Override
    public Set<K> keySet() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }

    @Override
    public Set<java.util.Map.Entry<K, V>> entrySet() {
        return null;
    }

}
like image 23
Pavan P Avatar answered May 06 '23 17:05

Pavan P