什么是 LinkedHashMap<k, v>? [英] What is LinkedHashMap<k, v>?

查看:14
本文介绍了什么是 LinkedHashMap<k, v>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是这些 HashMap 的新手,但对 LinkedLists 和 HashMap 有一些了解.如果你能给我一些关于 LinkedHashMap 的简单解释就好了,正如标题中那样,这是否意味着我们明确地将它定义为某种类型?

Ok so i am new to these HashMaps but have some idea about LinkedLists and HashMaps. It would be great if you could give me some simple explanation regarding LinkedHashMap and as in the titile does this mean we are explicitly defining it to be of some type?

推荐答案

LinkedHashMap 是以下各项的组合哈希表和链表.它有一个可预测的迭代顺序(a la链表),但检索速度是一个HashMap.的顺序迭代由插入顺序,所以你会得到键/值按照它们的顺序返回已添加到此地图中.你必须的这里有点小心,因为重新插入一键不改原订购.

A LinkedHashMap is a combination of hash table and linked list. It has a predictable iteration order (a la linked list), yet the retrieval speed is that of a HashMap. The order of the iteration is determined by the insertion order, so you will get the key/values back in the order that they were added to this Map. You have to be a bit careful here, since re-inserting a key does not change the original order.

k 代表 Key,v 代表 Value.

k stand for Key and v for Value.

/*
  Simple Java LinkedHashMap example
  This simple Java Example shows how to use Java LinkedHashMap.
  It also describes how to add something to LinkedHashMap and how to
  retrieve the value added from LinkedHashMap.
*/

import java.util.LinkedHashMap;

public class JavaLinkedHashMapExample {

public static void main(String[] args) {

//create object of LinkedHashMap
LinkedHashMap lHashMap = new LinkedHashMap();

/*
  Add key value pair to LinkedHashMap using
  Object put(Object key, Object value) method of Java LinkedHashMap class,
  where key and value both are objects
  put method returns Object which is either the value previously tied
  to the key or null if no value mapped to the key.
  */

lHashMap.put("One", new Integer(1));
lHashMap.put("Two", new Integer(2));

/*
  Please note that put method accepts Objects. Java Primitive values CAN NOT
  be added directly to LinkedHashMap. It must be converted to corrosponding
  wrapper class first.
  */

//retrieve value using Object get(Object key) method of Java LinkedHashMap class
Object obj = lHashMap.get("One");
System.out.println(obj);

/*
  Please note that the return type of get method is an Object. The value must
  be casted to the original class.
  */


}
}
/*
Output of the program would be
1
*/

这篇关于什么是 LinkedHashMap<k, v>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆