How to implement a list, set and map in a zero design?

This is great when you can return a null / empty object at any time to avoid zeros, but what about a collection as objects?

In Java Mapreturns null, if keyin get(key)is not found on the map.

The best thing to avoid nullin this situation is to return an object Entry<T>that is either EmptyEntry<T>or contains a value T.

Of course, we avoid null, but now you can get a class exception if you do not check if it is there EmptyEntry<T>.

Is there a better way to escape nullin Map get(K)?

And for the sake of argument, let's say, this language is not even there null, so don’t speak, just use it nulls.

+3
source share
11 answers

Two possible solutions:

  • Provide the contains (key) function. Throw an exception if get (key) is called for a nonexistent key. The downside is: calling get () after contains () duplicates the operations; inefficient.

  • Use of functional languages. Possible in such situations. This article explains how to implement Maybe in Java .

+5
source

", ", " ". , , , , , , , .

, , contains (key). , , get (key) null, !!!.

Edit:

get() - ( : 01:08 , !!)

  314       public V get(Object key) {
  315           if (key == null)
  316               return getForNullKey();
  317           int hash = hash(key.hashCode());
  318           for (Entry<K,V> e = table[indexFor(hash, table.length)];
  319                e != null;
  320                e = e.next) {
  321               Object k;
  322               if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
  323                   return e.value;
  324           }
                //This could be instanced by reflection (bad idea?)
  325           return new MappeableElementImpl();
   }

V - , MappeableElement, - , boolean isUnmappedValue(), get() .

, - :

Element e = map.get(notpresentkey);

if (e.isUnmappedValue()){sysout (notpresentkey  +" is not present");}
+2

- , , ? , , null (, ref obj, null), . , , (y) . !

+2

. .NET KeyNotFoundException, Java ArrayIndexOutOfBoundsException Python KeyError.

, , .

if collection.contains(key):
    return collection.get(key);

Bad

try:
    return collection.get(key);
catch KeyError:
    pass # Don't care.
+1

Optional<T>.

+1

, .

  • a null .
  • .
  • , containsKey, get.

, Null Object , , NullObjectFactory, , ( ). , get , , , , null, - .

+1

Cf. @Doug McClean - , Scala Option, Haskell Maybe. , , Entry EmptyEntry - Scala Some Entry None EmptyEntry.

Daniel Spiewak Option, Java. ( instanceof None , , isNone() , , None), Java- , , None<T> .)

+1

, , Found Item, , . - TryGet.

0

, null, , , , ( ..), null.

, , InvalidValueException ( ) NoValuePresentException get(). ( , , )

0

. , . , null. :

class MapAdapter<K,V> implements Map<K,V> {
    private Map<K,V> inner = new HashMap<K,V>();
    private final V nullObject;

    private MapAdapter(V nullObject) {
        this.nullObject = nullObject;
    }

    public static <K,V> Map<K,V> adapt(Map<K,V> mapToAdapt, V nullObject) {
        MapAdapter<K,V> adapter = new MapAdapter<K,V>(nullObject);
        adapter.inner.addAll(mapToAdapt);
        return adapter;
    }


    //Here comes implementation of methods delegating to inner.


   public V get(K key) {
       if (inner.containsKey(key)) {
           return inner.get(key);
       }
       return nullObject;
   }
}

, NullSafe.

0
source

Maybe and Option seem to be the way to go.

However, pattern matching is also necessary to simplify this class for the user. Thus, the user does not need to use instanceof and pour with the risk of throwing exceptions in real time.

0
source

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


All Articles