Map implementation with duplicate keys

后端 未结 18 1918
暗喜
暗喜 2020-11-22 15:23

I want to have a map with duplicate keys.

I know there are many map implementations (Eclipse shows me about 50), so I bet there must be one that allows this. I know

18条回答
  •  爱一瞬间的悲伤
    2020-11-22 15:50

    class  DuplicateMap 
    {
        enum MapType
        {
            Hash,LinkedHash
        }
    
        int HashCode = 0;
        Map,V> map = null;
    
        DuplicateMap()
        {
            map = new HashMap,V>();
        }
    
        DuplicateMap( MapType maptype )
        {
            if ( maptype == MapType.Hash ) {
                map = new HashMap,V>();
            }
            else if ( maptype == MapType.LinkedHash ) {
                map = new LinkedHashMap,V>();
            }
            else
                map = new HashMap,V>();
        }
    
        V put( K key, V value  )
        {
    
            return map.put( new Key( key , HashCode++ ), value );
        }
    
        void putAll( Map map1 )
        {
            Map,V> map2 = new LinkedHashMap,V>();
    
            for ( Entry entry : map1.entrySet() ) {
                map2.put( new Key( entry.getKey() , HashCode++ ), entry.getValue());
            }
            map.putAll(map2);
        }
    
        Set> entrySet()
        {
            Set> entry = new LinkedHashSet>();
            for ( final Entry, V> entry1 : map.entrySet() ) {
                entry.add( new Entry(){
                    private K Key = entry1.getKey().Key();
                    private V Value = entry1.getValue();
    
                    @Override
                    public K getKey() {
                        return Key;
                    }
    
                    @Override
                    public V getValue() {
                        return Value;
                    }
    
                    @Override
                    public V setValue(V value) {
                        return null;
                    }});
            }
    
            return entry;
        }
    
        @Override
        public String toString() {
            StringBuilder builder = new  StringBuilder();
            builder.append("{");
            boolean FirstIteration = true;
            for ( Entry entry : entrySet() ) {
                builder.append( ( (FirstIteration)? "" : "," ) + ((entry.getKey()==null) ? null :entry.getKey().toString() ) + "=" + ((entry.getValue()==null) ? null :entry.getValue().toString() )  );
                FirstIteration = false;
            }
            builder.append("}");
            return builder.toString();
        }
    
        class Key
        {
            K1 Key;
            int HashCode;
    
            public Key(K1 key, int hashCode) {
                super();
                Key = key;
                HashCode = hashCode;
            }
    
            public K1 Key() {
                return Key;
            }
    
            @Override
            public String toString() {
                return  Key.toString() ;
            }
    
            @Override
            public int hashCode() {
    
                return HashCode;
            }
        }
    

提交回复
热议问题