private void putToMap(final K key, final GeoItem item, final GeoItem oldItem) {

        if (item == null && oldItem == null) {
            throw new IllegalArgumentException("Progamming bug: either item or oldItem must be non-null for: " + key);
        }

        if (mapWriter == null) {
            Log.d("Map writer not yet initialized");
            return;
        }

        //GeoPrimitives are handled different from GeoGroups
        //- Primitives are inserted with one element with index 0
        //- for GeoGroups, the contained primitives are inserted with an index matching their hashcode (this makes replacements more efficient)
        //=> if for same key a switch is done between primitive and geogroup, this requires special handling

        final boolean itemIsPrimitive = item instanceof GeoPrimitive;
        final boolean oldItemIsPrimitive = oldItem instanceof GeoPrimitive;

        if (itemIsPrimitive && (oldItem == null || oldItemIsPrimitive)) {
            //->insert or replace one GeoPrimitive with another
            mapWriter.put(key, (GeoPrimitive) item);
        } else if (item == null && oldItemIsPrimitive) {
            //->remove a GeoPrimitive
            mapWriter.remove(key);
        } else if (!itemIsPrimitive && !oldItemIsPrimitive) {
            //->insert or replace a GeoGroup with another, or remove a GeoGroup
            replaceGroupInMap(key, oldItem, item);
        } else if (itemIsPrimitive) {
            //->replace a GeoGroup with a GeoPrimitive
            replaceGroupInMap(key, oldItem, null);
            mapWriter.put(key, (GeoPrimitive) item);
        } else {
            //->replace a GeoPrimitive with a GeoGroup
            mapWriter.remove(key);
            replaceGroupInMap(key, null, item);
        }
    }
Replaces a set of items (identified by keysToReplace) with the given items. Prevents unnecessary remove+put for same key
main/src/main/java/cgeo/geocaching/unifiedmap/geoitemlayer/GeoItemLayer.java