简体   繁体   English

Linkedhaspmap 和linkedhashmap 的区别<Integer,String>

[英]Difference between linkedhaspmap and linkedhashmap<Integer,String>

I have created two maps as shown below.我创建了两个地图,如下所示。 Can any one explain why the output of the program is like this and why deletecontent method allowed me to pass mymap2?谁能解释一下为什么程序的输出是这样的,为什么deletecontent方法允许我通过mymap2? Difference between creating mymap and mymap2创建 mymap 和 mymap2 的区别

   public class Candidate {

            public static void main(String[] args) {
                // TODO Auto-generated method stub
                LinkedHashMap<Integer, String> mymap= new LinkedHashMap<Integer, String>();
                mymap.put(1, "INDIA");
                mymap.put(2, "USA");
                mymap.put(3, "RUSSIA");

                LinkedHashMap mymap2= new LinkedHashMap();
                mymap2.put("1", "INDIA");
                mymap2.put("2", "USA");
                mymap2.put("3", "RUSSIA");

                deleteContent(mymap);
                deleteContent(mymap2);
                print(mymap);
                System.out.println("------------------");
                print(mymap2);

            }

            private static void print(LinkedHashMap<Integer, String> mymap) {
                for (Entry<Integer, String> e: mymap.entrySet()) {
                    System.out.println(e.getKey()+"-----"+ e.getValue());
                }

            }

            private static void deleteContent(LinkedHashMap<Integer, String> mymap) {
                // TODO Auto-generated method stub
                mymap.remove("3");
            }


        }

    output of the below program is 
    1-----INDIA
    2-----USA
    ------------------
    1-----INDIA
    2-----USA
    3-----RUSSIA

The output you put in your question is not the output this code actually produces, which is :您在问题中输入的输出不是此代码实际产生的输出,即:

1-----INDIA
2-----USA
3-----RUSSIA
------------------
1-----INDIA
2-----USA

The second Map has String keys and String values.第二个 Map 具有 String 键和 String 值。 Therefore, only for the second Map mymap.remove("3");因此,仅适用于第二个 Map mymap.remove("3"); removes an entry from the Map.从地图中删除一个条目。

mymap.remove("3"); passed compilation, even though mymap is a LinkedHashMap<Integer, String> , since remove accepts a parameter of type Object .通过编译,即使mymapLinkedHashMap<Integer, String> ,因为remove接受Object类型的参数。

deleteContent allows you to pass a raw LinkedHashMap type to it due to backwards compatibility.由于向后兼容, deleteContent允许您将原始LinkedHashMap类型传递给它。 You might have old (pre-Java 5) methods that return raw types, so you are allowed to pass them to newer methods that expect parameterized types.您可能有返回原始类型的旧(Java 5 之前)方法,因此您可以将它们传递给需要参数化类型的较新方法。

Well, 3 is not the same as "3" (one is an Integer and the other is a String ) - that's why the output is different - the first map has no key to remove.好吧, 3"3" (一个是Integer ,另一个是String )——这就是输出不同的原因——第一个映射没有要删除的键。

That the code actually compiles is a surprise to me as well.代码实际编译对我来说也是一个惊喜。 I guess that's another reason for not using raw types anymore.我想这是不再使用原始类型的另一个原因。

Also, for me the output is另外,对我来说,输出是

1-----INDIA
2-----USA
3-----RUSSIA
------------------
1-----INDIA
2-----USA

not the other way around.反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM