下面的方法是如何工作的?
Pairs 是一个 LinkedHashMap,我只是不确定增强的 for 循环以及它如何与 hasmap 一起工作。即键集。
/**
* Finds all names in the initial input list that match phonetically with
* the supplied name
*
* @param phoneticName
* The name for which you want to find matches
* @return An ArrayList of all phonetically matching names
*/
public ArrayList<String> findMatchingNames(String phoneticName) {
ArrayList<String> matchedNames = new ArrayList<>();
for (String s : Pairs.keySet()) {
if (phoneticName.equals(Pairs.get(s))) {
matchedNames.add(s);
}
}
return matchedNames;
}
}
最佳答案
该方法遍历LinkedHashMap
中当前的所有键:
for (String s : Pairs.keySet()) {
如果映射中与此键关联的值等于传递的参数,我们会将此键保存在列表中matchedNames
:
if (phoneticName.equals(Pairs.get(s))) {
matchedNames.add(s);
}
然后我们返回键列表,其值等于传递的参数phoneticName
。
关于java - LinkedHashMap,方法内的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415603/