简体   繁体   English

什么是LinkedHashMap及其用途是什么?

[英]What is LinkedHashMap and what is it used for?

When I was going through a example code which has ListViews I came up with LinkedHashMap . 当我浏览一个包含ListViews的示例代码时,我提出了LinkedHashMap What is a LinkedHashMap and where can we use it and how? 什么是LinkedHashMap ,我们在哪里可以使用它以及如何使用它? I went through several articles but did not understand fully. 我经历了几篇文章,但没有完全理解。 Is it necessary when creating ListView . 创建ListView时是否有必要。 What is the connection between ListViews and LinkedHashMaps? ListViews和LinkedHashMaps之间有什么联系? Thank you. 谢谢。

The docs are here. 文档在这里。 But its basically a HashMap that also has a linked list, so you can have a consistently ordered iteration through it. 但它基本上是一个HashMap,它也有一个链表,所以你可以通过它进行一致的迭代迭代。 Note that this means removals may be O(n) time because you need to remove it from both data structures. 请注意,这意味着删除可能是O(n)时间,因为您需要从两个数据结构中删除它。

For Simplicity, let us understand what is the difference between HashMap and LinkedHashMap . 对于Simplicity,让我们了解HashMapLinkedHashMap之间的区别。

HashMap : It gives output in Random orders means there is no proper sequence how we have inserted values. HashMap :它以随机顺序给出输出意味着没有正确的顺序我们如何插入值。

whereas

LinkedHashMap : It gives output in sequential order. LinkedHashMap :它按顺序提供输出。

Let us see a small example: with HashMap 让我们看一个小例子:使用HashMap

    // suppose we have written a program
    .
    .
    // now use HashMap
    HashMap map = new HashMap();  // create object
    map.put(1,"Rohit");          // insert values
    map.put(2,"Rahul");
    map.put(3,"Ajay");

    System.out.println("MAP=" +map); //print the output using concatenation


    //So the output may be in any order like we can say the output may be as:
    Map={3=Ajay,2=Rahul,1=Rohit}

but this is not the case in LinkedHashMap Just replace the "HashMap" with "LinkedHashMap" in the above code and see it will display the output in Sequential order like 1=Rohit will be displayed first then the others in sequence. 但是在LinkedHashMap中不是这种情况只需在上面的代码中用“LinkedHashMap”替换“HashMap”并看到它将按顺序显示输出,如1 = Rohit将首先显示,然后按顺序显示。

LinkedHashMap is hashmap. LinkedHashMap是hashmap。 But it maintains order of insertion. 但它维持插入顺序。 But HashMap doesnt maintain order. 但是HashMap没有维持秩序。

Hi Linked Hash Map is a Map which stored key value pair, Linked Hash Map add the values may very slow, But while retrieving the values is very easy. Hi Linked Hash Map是一个存储键值对的Map,Linked Hash Map添加的值可能很慢,但是检索值非常容易。 For fast retrieval of values we could prefer Linked Hash Map. 为了快速检索值,我们可能更喜欢Linked Hash Map。

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

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