Created
April 16, 2020 17:27
-
-
Save amirmv2006/d8b224612ab1e2fec933a40bc2afb8ab to your computer and use it in GitHub Desktop.
a Pair of two values, useful for Streams
This file contains hidden or 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 ir.amv.enterprise.laas.webapp.utils.fn; | |
import lombok.Value; | |
import java.util.Comparator; | |
import java.util.Map; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
@Value | |
public class Pair<K, V> { | |
K key; | |
V value; | |
public <K2> Pair<K2, V> mapKey(Function<K, K2> fn) { | |
return new Pair<>(fn.apply(key), value); | |
} | |
public <V2> Pair<K, V2> mapValue(Function<V, V2> fn) { | |
return new Pair<>(key, fn.apply(value)); | |
} | |
public <K2> Pair<K2, V> mapKey(BiFunction<K, V, K2> fn) { | |
return new Pair<>(fn.apply(key, value), value); | |
} | |
public <V2> Pair<K, V2> mapValue(BiFunction<K, V, V2> fn) { | |
return new Pair<>(key, fn.apply(key, value)); | |
} | |
public static <K, V> Comparator<Pair<K, V>> comparingKey(Comparator<K> keyComparator) { | |
return Comparator.comparing(Pair::getKey, keyComparator); | |
} | |
public static <K, V> Comparator<Pair<K, V>> comparingValue(Comparator<V> valueComparator) { | |
return Comparator.comparing(Pair::getValue, valueComparator); | |
} | |
public static <K, V> Pair<K, V> fromMapEntry(final Map.Entry<K, V> mapEntry) { | |
return new Pair<>(mapEntry.getKey(), mapEntry.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment