@@ -0,0 +1,2217 @@
+package Hashing;%0A%0Aimport java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Objects;%0A%0A/**%0A * HashMap like datastructure with one to one mapping between keys and values%0A * Bijective Map%0A * getKey(k) and getValue(v) in O(1)%0A */%0Apublic class BiMap%3CT1, T2%3E %7B%0A    static class Entry%3CT1, T2%3E%7B%0A        public T1 key;%0A        public T2 value;%0A%0A        public Entry(T1 key, T2 value)%7B%0A            this.key = key;%0A            this.value = value;%0A        %7D%0A    %7D%0A%0A    private HashMap%3CObject, Entry%3CT1, T2%3E%3E mappings = new HashMap%3C%3E();%0A%0A    public void put(T1 key, T2 value)%7B%0A        remove(key);%0A        remove(value);%0A        Entry%3CT1, T2%3E entry = new Entry%3C%3E(key, value);%0A        if(Objects.equals(key, value))%7B%0A            mappings.put(key, entry);%0A        %7D else %7B%0A            mappings.put(key, entry);%0A            mappings.put(value, entry);%0A        %7D%0A%0A    %7D%0A%0A    // Hashmap lookup using either the key or the value should return the key-value entry%0A    public Entry%3CT1, T2%3E get(Object keyValue)%7B%0A        return mappings.get(keyValue);%0A    %7D%0A%0A    public Entry%3CT1, T2%3E remove(Object keyValue) %7B%0A        Entry%3CT1, T2%3E entry = mappings.remove(keyValue);%0A        if(entry == null) return null;%0A        if(Objects.equals(keyValue, entry.key))%7B%0A            return mappings.remove(entry.value);%0A        %7D else %7B%0A            return mappings.remove(entry.key);%0A        %7D%0A    %7D%0A%0A    public int size()%7B%0A        return mappings.size();%0A    %7D%0A%0A    public String toString()%7B%0A        StringBuilder sb = new StringBuilder();%0A        for (Map.Entry%3CObject, Entry%3CT1, T2%3E%3E entry : mappings.entrySet()) %7B%0A            sb.append(entry.getKey().toString() + %22 -%3E Entry(%22 + entry.getValue().key.toString()%0A                    + %22, %22 + entry.getValue().value.toString() + %22)%22);%0A            sb.append(%22%5Cn%22);%0A        %7D%0A%0A        return sb.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        BiMap%3CString, String%3E bimap = new BiMap%3C%3E();%0A        bimap.put(%22x%22, %22y%22);%0A        bimap.put(%22a%22, %22a%22);%0A        System.out.println(bimap.toString());%0A%0A        bimap.remove(%22x%22);%0A        System.out.println(bimap.toString());%0A%0A        bimap.put(%22a%22, %22b%22);%0A        System.out.println(bimap.toString());%0A    %7D%0A%7D%0A

Implementation of a bijective one to one hashmap