【发布时间】:2020-03-11 16:15:54
【问题描述】:
我有一个这样的Map<Pair<String, String>, Integer>:
Pair.left Pair.right Integer
1 A 10
1 B 20
2 C 30
现在,如果我像 1 一样传递 Pair.left 值,那么我应该在地图中获得 Pair.right 和该整数:
Map<String, Integer> :
A 10
B 20
If i pass 2,
then
C 30
所以,我试试这个:
public Map<String, Integr> foo(Map<Pair<String, String>, Integer> input, String LeftValue)
{
Map<String, Integer> result = new HashMap();
Set<Pair<String, String>> inputKeysets = input.keySet();
//Now, I am thinking i will loop through the inputKeysets and see if the getLeft() matches the LeftValue, if it does then i will take the getRight() and store in a new Set
//Then i will have LeftValue and RightValue and then will compare again from the input and get the Integer value
}
lambda 有什么简单的方法可以做到这一点吗?
【问题讨论】:
-
如果您只对基于
Pair.left的值感兴趣,那么为什么要使用整个Pair类作为映射键? -
因为基于 Pair.left 我会知道要传递什么并且它已经在其他几个地方使用过。我需要在几个地方映射 pair.left 和 pair.right。