Generics and collections - undefined method for type

I am working on a project and I am a bit stuck in one aspect of it. I need to create a class that can manage sets of instances of other classes. As part of this, he creates a wrapper object for each instance, which should be able not only to hold the instance, but to perform certain operations on it (including equals / hashcode).

Thus, the constructor accepts not only a wrapped object, but also a function object, which, according to the caller, can perform these operations as desired (which may differ from the native behavior for the contained objects).

By the way, I know that what I am describing here is similar to what I am inventing part of the Collections framework, but I have simplified here.


public class MapWrapper<K,V> {
    private class KeyWrapper<K> {
        K key;
        public KeyWrapper(K key) {
            // ...
        }
    }
    private class ValueWrapper<V> {
        V value;
        public ValueWrapper(V value) {
            // ...
        }
    }

    // ...
    HashMap<KeyWrapper<K>, ValueWrapper<V>> map 
            = new HashMap<KeyWrapper<K>, ValueWrapper<V>> ();
    // ...

, , , , :


    public MapWrapper (HashMap<K, V> map) {
        // ...
        map.put(new KeyWrapper<K>(key), new ValueWrapper<V>(val));
        // ...
    }

:

  • " put (K, V) HashMap < K, V > (HashPlus.KeyWrapper, HashPlus.ValueWrapper)

. ? - , HashMap<Object,Object> - ​​ HashMap<K,V>, .

? ?

: , "":


        this.map.put(new KeyWrapper<K>(key), new ValueWrapper<V>(val));

. : , KeyWrapper ValueWrapper put, , "raw type", .

.

+3
2
  • it put(..) not add(..)
  • KeyWrapper ValueWrapper , . this.map.put(..)
  • KeyWrapper ValueWrapper. K V MapWrapper
+4

. , , put, add.

+5

Source: https://habr.com/ru/post/1719272/


All Articles