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

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

问题描述

好的,我对这些HashMaps很陌生,但对LinkedLists和HashMaps有一些想法。
如果你可以给我一些关于LinkedHashMap的简单解释,并且在titile中,这是否意味着我们明确地将它定义为某种类型呢?

LinkedHashMap是
散列表和链表的组合。它有一个
可预测的迭代次序(la
链表),但检索速度
是HashMap的检索速度。
迭代的顺序由
广告订单确定,因此您将按
被添加到此地图的顺序返回
键值。您必须在这里谨慎
a,因为重新插入
a键并不会更改原来的
单。




代表Key,v代表Value。

  / * 
简单Java LinkedHashMap示例
这个简单的Java示例演示了如何使用Java LinkedHashMap。
它还描述了如何向LinkedHashMap添加内容以及如何
检索从LinkedHashMap添加的值。
* /

import java.util.LinkedHashMap;

public class JavaLinkedHashMapExample {

public static void main(String [] args){

//创建LinkedHashMap的对象
LinkedHashMap lHashMap = new LinkedHashMap();


使用
将键值对添加到LinkedHashMap中Java的LinkedHashMap类的对象put(Object key,Object value)方法,
key和value都是objects
put方法返回Object,它是之前将值
绑定到键的值,如果没有值映射到键,则返回null。
* /

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

/ *
请注意,put方法接受对象。 Java原始值CAN NOT
直接添加到LinkedHashMap。它必须首先转换为相应的
包装类。
* /

//使用Object的get(Object key)方法获取值LinkedHashMap类
Object obj = lHashMap.get(One);
System.out.println(obj);

/ *
请注意get方法的返回类型是一个Object。值必须
转换为原始类。
* /


}
}
/ *
该方案的产出将为
1
* /


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?

解决方案

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 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天全站免登陆