在java中使用HashMap的主要好处是什么? [英] What is the main benefit of using HashMap in java?

查看:1216
本文介绍了在java中使用HashMap的主要好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

HashMap和Hashtable之间的差异


在这个Java项目中我正在看,我一直看到HashMap的代码,像这样

  / ** imageID  - > image map * / 
Map< String,ImageIcon> imgs = new HashMap< String,ImageIcon>();

然后在课后:

  //图像
loadImages();
actualImage = imgs.get(this.DEFAULT_IMAGE_ID);
JLabel label = new JLabel(actualImage);

此代码的目的是什么?我在这里的整个概念是有雾的。谢谢

解决方案

两者都提供对数据的键值访问。 Hashtable是Java中的原始集合类之一。 HashMap是新的Collections Framework的一部分,添加了Java 2,v1.2。



两者之间的主要区别是在表上同步访问Hashtable而访问HashMap则不是。您可以添加它,但默认情况下不存在。



另一个区别是HashMap中的迭代器是故障安全的,而Hashtable的枚举器不是。如果您在迭代时更改地图,您将知道。



另外,第三个区别是HashMap允许在其中使用空值,而Hashtable不允许。 p>

回答编辑过的问题:

  / ** imageID  - > image map * / 
// imageID - String。 imgs是imageID和ImageIcon的映射。 imageID是关键。 ImageIcon是值。
Map< String,ImageIcon> imgs = new HashMap< String,ImageIcon>();

然后在课后:

  //查看内联评论
//图片
//没有提供定义。可能把价值观放在imgs地图上。
loadImages();
//this.DEFAULT_IMAGE_ID是一些imageID。 imgs.get获取该imageID的值,其中
//是该ImageID的ImageIcon。它存储在actualImage变量中。
actualImage = imgs.get(this.DEFAULT_IMAGE_ID);
//使用actualImage创建一个新的JLabel。
JLabel label = new JLabel(actualImage);


Possible Duplicate:
Differences between HashMap and Hashtable?

In this Java project I'm looking at, I keep seeing code with HashMap, like so

 /** imageID --> image map */
    Map<String,ImageIcon> imgs = new HashMap<String,ImageIcon>();

Then later in the class:

// images 
loadImages();
actualImage = imgs.get(this.DEFAULT_IMAGE_ID);
JLabel label = new JLabel(actualImage);

What is the purpose of this code? I am foggy on the whole concept here. Thanks

解决方案

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.

Answer to your edited question:

/** imageID --> image map */
//imageID - String. imgs is a map of imageID and ImageIcon. imageID is key. ImageIcon is value.
    Map<String,ImageIcon> imgs = new HashMap<String,ImageIcon>();

Then later in the class:

//SEE INLINE COMMENTS
// images 
//No definition provided. May be putting values into the imgs map.
loadImages();
//this.DEFAULT_IMAGE_ID is some imageID. imgs.get gets the value for that imageID, which
//is ImageIcon for that imageID. That is stored in actualImage variable.
actualImage = imgs.get(this.DEFAULT_IMAGE_ID);
//Creating a new JLabel with actualImage.
JLabel label = new JLabel(actualImage);

这篇关于在java中使用HashMap的主要好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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