Skip to content

Instantly share code, notes, and snippets.

@albertoventurini
Created September 29, 2017 09:48
Show Gist options
  • Save albertoventurini/5c31c4c143555999d6613d0a0a36387a to your computer and use it in GitHub Desktop.
Save albertoventurini/5c31c4c143555999d6613d0a0a36387a to your computer and use it in GitHub Desktop.
An implementation of Java's AbstractMap that returns the same value for all keys.
package com.amazonaws.ec2.tagging.util;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class SingleValueMap<K, V> extends AbstractMap<K, V> {
private final Set<K> keys;
private final V value;
public SingleValueMap(final Collection<K> keys, final V value) {
this.keys = new HashSet<>(keys);
this.value = value;
}
@Override
public Set<Entry<K, V>> entrySet() {
return keys.stream()
.map(key -> new AbstractMap.SimpleImmutableEntry<K, V>(key, this.value))
.collect(Collectors.toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment