Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LinkedHashMap,方法中的功能?_Java_Arraylist_Linkedhashmap - Fatal编程技术网

Java LinkedHashMap,方法中的功能?

Java LinkedHashMap,方法中的功能?,java,arraylist,linkedhashmap,Java,Arraylist,Linkedhashmap,下面的方法是如何工作的 Pairs是一个LinkedHashMap,我只是不确定增强的for循环以及它如何和hasmap一起工作。i、 打开钥匙组 /** * Finds all names in the initial input list that match phonetically with * the supplied name * * @param phoneticName *

下面的方法是如何工作的

Pairs是一个LinkedHashMap,我只是不确定增强的for循环以及它如何和hasmap一起工作。i、 打开钥匙组

/**
         * 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;
        }
    }
/**
*在初始输入列表中查找语音上与匹配的所有名称
*提供的名称
* 
*@param语音名称
*要查找匹配项的名称
*@返回所有语音匹配名称的ArrayList
*/
公共ArrayList findMatchingNames(字符串名称){
ArrayList matchedNames=新的ArrayList();
对于(字符串s:Pairs.keySet()){
if(拼音name.equals(Pairs.get))){
匹配名称。添加;
}
}
返回匹配的名称;
}
}

该方法遍历当前在
LinkedHashMap
中的所有键:

for (String s : Pairs.keySet()) {
如果映射中与此键关联的值等于传递的参数,则将此键保存在列表
matchedNames
中:

if (phoneticName.equals(Pairs.get(s))) {
    matchedNames.add(s);
}

然后我们返回键列表,其值等于传递的参数
拼音名

基本上增强了for循环与所有可编辑的集合一起工作

所以在我们的例子中,如果我们使用LinkedHashMap,它就不能直接使用,因为 1) 它不是一个集合,而是一个键值对

2) 我们不能通过迭代直接获取键值

所以我们需要把钥匙收集起来,比如说集合

为什么设置?因为map将具有唯一(无重复)键,所以它们实现了一个方法 在一个名为keySet的映射中

因此,map.keySet()返回键集

现在我们可以轻松地使用for循环进行迭代,因为集合是可迭代的

所以我们写了

for (String s : Pairs.keySet())
现在,上述语法中的每个s都是每次迭代中一个接一个来自keySet的字符串

现在将传递的名称与映射中相应键的值进行比较,如果它们相等,则将该名称添加到列表中

Pairs.get(s)
->给出键s的映射值

if (phoneticName.equals(Pairs.get(s))) {
    matchedNames.add(s);
}

在方法的末尾,返回这个新形成的匹配列表。

enhanced for loop适用于实现
java.lang.Iterable