Created
September 29, 2017 09:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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