IgnoreEmptyKeyMapDecorator implements Map<String, V> { @Override public V put(String key, V value) { if (!Strings.isNullOrEmpty(key)) { return delegate.put(key, value); } else { return null; } } IgnoreEmptyKeyMapDecorator(Map<String, V> delegate); @Override int size(); @Override boolean isEmpty(); @Override boolean containsKey(Object key); @Override boolean containsValue(Object value); @Override V get(Object key); @Override V put(String key, V value); @Override V remove(Object key); @Override void putAll(Map<? extends String, ? extends V> m); @Override void clear(); @NotNull @Override Set<String> keySet(); @NotNull @Override Collection<V> values(); @NotNull @Override Set<Entry<String, V>> entrySet();  }
@Test public void testValueNotAddedIfKeyIsNullOrEmpty() { Map<String, String> delegate = new HashMap<>(); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); assertNull("key should not be mapped", ignoreEmptyKeyMap.put("", "newValue")); assertNull("key should not be mapped", ignoreEmptyKeyMap.put(null, "newValue")); }