i具有hashmap,其键是字符串,值存储为列表。
Map<String,List<Integer>> posmap = new HashMap<>();
因为我在同一键下有多个值。例如,我有4个键,值是:
[1681,3523]
[276,489]
[1527,2865]
[300]
我还有另一个包含这些值的treelist。我想做的是(并问);
Iterator<Integer> itr=myTreeSet.iterator();
while(itr.hasNext())
{
int check = itr.next();
for(Object value : map2.entrySet())
{
//System.out.println("Value is :" + value);
}
}
我想使用我的hashmap输入集中的代码中的ITR检查,如果等于返回hashmap键。简要地检查ITR属于Hashmap的输入集,并属于哪个键。
我将MAP1和MAP2作为参数
static void game(Map map1, Map map2, Hero h, HashSet< Integer> hash)
我很抱歉。我不清楚,我想你很困惑。我首先在主弹药中定义了此地图:
Map<String, Enemy> enemymap = new HashMap<>();
Map<String,List<Integer>> posmap = new HashMap<>();
我也将它们填充在主要弹药中。填写后,我将它们发送到了我的功能游戏中。POSMAP是Map2。所以我指的是posmap。对不起,困惑。弹性的完整代码。
static void game(Map map1, Map map2, Hero h, HashSet< Integer> hash)
{
TreeSet<Integer> myTreeSet = new TreeSet<>(); // For sorting min to max-for alignment
myTreeSet.addAll(myHashset);
System.out.println(myTreeSet);
String start = "Micheal started travel with" + " " + h.getHealth() + " " + "HP!";
Iterator<Integer> itr=myTreeSet.iterator();
while(itr.hasNext())
{
int check = itr.next();
/* for(Map.Entry<String, List<Integer>> entry : map2.entrySet())
{
if(entry.getValue() != null && entry.getValue().contains(check))
System.out.println("The key " + entry.getKey() + "contains the treeset value " + check);
} -/
}
}
从主弹药发送AS:
game(enemymap, posmap , hero, myHashset);
其他解决方案打印或返回单键。根据您简短的原始帖子,我认为您想要一套,所以这就是我制作的。
用 @ajb的建议编辑:
Set<Integer> myTreeSet = new TreeSet<>();
Map<String,List<Integer>> posmap = new HashMap<>();
Iterator<Integer> itr = myTreeSet.iterator();
//The set of keys containing values in any list of posmap
Set<String> matchedKeys = new HashSet<>();
//Iterate through the TreeSet Integers
itr.forEachRemaining(itrVal -> {
//Stream each entry of the posmap
posmap.entrySet().stream()
//Remove the entries without the itrVal in the entry's list
.filter(entry -> entry.getValue().contains(itrVal))
//Add each key with a match to the set
.forEach(matchedEntry -> matchedKeys.add(matchedEntry.getKey()));
});
return matchedKeys;
如果您可以使用Apache Commons,这可能是一种很好的方法:
Set<Integer> myTreeSet = new TreeSet<>();
Map<String,List<Integer>> posmap = new HashMap<>();
Iterator<Integer> itr = myTreeSet.iterator();
Set<String> matchedKeys = new HashSet<>();
List<Integer> treeSetValues = IteratorUtils.toList(itr);
treeSetValues.stream().map(val -> {
return posmap.entrySet().stream()
.filter(entry -> entry.getValue().contains(itrVal))
.collect(Collectors.toSet());
});
return matchedKeys;
如果您不能使用iteratoritils.tolist(),则可以使用guava and lists.newarraylist()。
我会推荐以下方法:
Iterator<Integer> itr=myTreeSet.iterator();
while(itr.hasNext())
{
int check = itr.next();
for(Map.Entry<String, List<Integer>> entry : map2.entrySet())
{
if(entry.getValue() != null && entry.getValue().contains(check))
System.out.println("The key " + entry.getKey() + "contains the treeset value " + check);
}
}
,因为您不记得在hashmap中存储int时使用的hashkey,因此您需要检查,如果int在哈希的任何值中。
for(Map.Entry<String,List<Integer>>entry: map2.values()) {
if( entry.getValue().contains(check) ) return entry.getKey();
}
类似的东西。