@Test public void testTransformingMapViewContent() { Map<String, String> stringMap = new HashMap<>(); Map<String, Integer> intMap = Views.mapView(stringMap, Integer::parseInt); stringMap.put("key1", "1"); stringMap.put("key2", "2"); stringMap.put("key3", "3"); assertEquals(Integer.valueOf(1), intMap.get("key1")); assertEquals(Integer.valueOf(2), intMap.get("key2")); assertEquals(Integer.valueOf(3), intMap.get("key3")); stringMap.remove("key2"); assertEquals(Integer.valueOf(1), intMap.get("key1")); assertEquals(Integer.valueOf(3), intMap.get("key3")); }
public static <K, V, O> Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer) { return new AbstractMap<K, V>() { @Override public Set<Entry<K, V>> entrySet() { return new AbstractSet<Entry<K, V>>() { Set<Entry<K, O>> originalSet = originalMap.entrySet(); Iterator<Entry<K, O>> original = originalSet.iterator(); @Override public Iterator<Entry<K, V>> iterator() { return new Iterator<Entry<K, V>>() { @Override public boolean hasNext() { return original.hasNext(); } @Override public Entry<K, V> next() { Entry<K, O> next = original.next(); return new Entry<K, V>() { @Override public K getKey() { return next.getKey(); } @Override public V getValue() { return valuesTransformer.apply(next.getValue()); } @Override public V setValue(V value) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public void remove() { original.remove(); } }; } @Override public int size() { return originalSet.size(); } }; } }; }
Views { public static <K, V, O> Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer) { return new AbstractMap<K, V>() { @Override public Set<Entry<K, V>> entrySet() { return new AbstractSet<Entry<K, V>>() { Set<Entry<K, O>> originalSet = originalMap.entrySet(); Iterator<Entry<K, O>> original = originalSet.iterator(); @Override public Iterator<Entry<K, V>> iterator() { return new Iterator<Entry<K, V>>() { @Override public boolean hasNext() { return original.hasNext(); } @Override public Entry<K, V> next() { Entry<K, O> next = original.next(); return new Entry<K, V>() { @Override public K getKey() { return next.getKey(); } @Override public V getValue() { return valuesTransformer.apply(next.getValue()); } @Override public V setValue(V value) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public void remove() { original.remove(); } }; } @Override public int size() { return originalSet.size(); } }; } }; } }
Views { public static <K, V, O> Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer) { return new AbstractMap<K, V>() { @Override public Set<Entry<K, V>> entrySet() { return new AbstractSet<Entry<K, V>>() { Set<Entry<K, O>> originalSet = originalMap.entrySet(); Iterator<Entry<K, O>> original = originalSet.iterator(); @Override public Iterator<Entry<K, V>> iterator() { return new Iterator<Entry<K, V>>() { @Override public boolean hasNext() { return original.hasNext(); } @Override public Entry<K, V> next() { Entry<K, O> next = original.next(); return new Entry<K, V>() { @Override public K getKey() { return next.getKey(); } @Override public V getValue() { return valuesTransformer.apply(next.getValue()); } @Override public V setValue(V value) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public void remove() { original.remove(); } }; } @Override public int size() { return originalSet.size(); } }; } }; } private  Views(); }
Views { public static <K, V, O> Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer) { return new AbstractMap<K, V>() { @Override public Set<Entry<K, V>> entrySet() { return new AbstractSet<Entry<K, V>>() { Set<Entry<K, O>> originalSet = originalMap.entrySet(); Iterator<Entry<K, O>> original = originalSet.iterator(); @Override public Iterator<Entry<K, V>> iterator() { return new Iterator<Entry<K, V>>() { @Override public boolean hasNext() { return original.hasNext(); } @Override public Entry<K, V> next() { Entry<K, O> next = original.next(); return new Entry<K, V>() { @Override public K getKey() { return next.getKey(); } @Override public V getValue() { return valuesTransformer.apply(next.getValue()); } @Override public V setValue(V value) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public void remove() { original.remove(); } }; } @Override public int size() { return originalSet.size(); } }; } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2); }
Views { public static <K, V, O> Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer) { return new AbstractMap<K, V>() { @Override public Set<Entry<K, V>> entrySet() { return new AbstractSet<Entry<K, V>>() { Set<Entry<K, O>> originalSet = originalMap.entrySet(); Iterator<Entry<K, O>> original = originalSet.iterator(); @Override public Iterator<Entry<K, V>> iterator() { return new Iterator<Entry<K, V>>() { @Override public boolean hasNext() { return original.hasNext(); } @Override public Entry<K, V> next() { Entry<K, O> next = original.next(); return new Entry<K, V>() { @Override public K getKey() { return next.getKey(); } @Override public V getValue() { return valuesTransformer.apply(next.getValue()); } @Override public V setValue(V value) { throw new UnsupportedOperationException("Not supported."); } }; } @Override public void remove() { original.remove(); } }; } @Override public int size() { return originalSet.size(); } }; } }; } private  Views(); static List<T> listView(List<R> originalList, Function<R, T> transformer); static Map<K, V> mapView(Map<K, O> originalMap, Function<O, V> valuesTransformer); static Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2); static Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2);  }