当前位置:首页 > 开发 > 互联网 > 正文

Difference between HashSet and HashMap in Java

发表于: 2013-08-14   作者:cywhoyi   来源:转载   浏览次数:
摘要: HashSet and HashMap in JavaHashSet vs HashMap is a classical Java Collection interview question which focuses on What are differences between HashSet and H

HashSet and HashMap in Java
HashSet vs HashMap is a classical Java Collection interview question which focuses on What are differences between HashSet and HashMap in terms of features, usage and performance. If you are in Java programming even for a year or so, you are likely to be familiar with What is HashSet in Java and What is HashMap in Java, these two are most popularcollection classes. Despite being hash based collection HashSet and HashMap are different to each other because underlying interface are different. HashSet implements Set interface via extending AbstractSetclass and HashMapimplements Map interface. Before seeing differences between let's see what is common between HashSet and HashMap inJava:


Similarities on HashMap and HashSet in Java
Here are some of the common stuff between both of them:

1) Both HashMap and HashSet are hash based collection in Java.
2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads.
3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throwConcurrentModificationException if they detect any structural change in Collection.

4) Both HashMap and HashSet provided constant time performance for basic operations like put(), get() etc.
5) Both HashSet and HashMap allows null values.

Differences between HashSet and HashMap in Java


After seeing similarities on  HashMap and  HashSet, now let's see some difference between them :
 
1) First and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality HashSet is a wrapper around HashMap in Java, if you look at  the code of add(E e)  method of HashSet.java you will see following code :

public boolean add(E e) {
        return map.put(e, PRESENT)==null;
}

where its putting  Object into map as key and value is an final object PRESENT which is dummy.

2) Second difference between  HashMap and  HashSet is that , we use add() method to put elements into Set but we use put() method to insert key and value into HashMap in Java.

3) HashSet allows only one null key, but HashMap can allow one null key + multiple null values.

That's all on  difference between HashSet and HashMap in Java. In summary HashSet and HashMap are two different type of Collection one being Set and other being Map.

 

Difference between HashSet and HashMap in Java

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Hashtable: 1. key和value都不许有null值 2. 使用enumeration遍历 3. 同步的,每次只有一个线程能够
有人说Java很简单都是api调用而已,没啥技术.其实光是Jdk提供的api中很多思想和算法都用在里面,比如
WCF has introduced lot of bindings and protocols. This article will concentrate on two import
WCF has introduced lot of bindings and protocols. This article will concentrate on two import
在前面我们已经介绍了题目中几个类分别实现了哪个接口。HashSet和TreeSet都直接或者间接的继承了Set
查看 HashSet的源码会发现,HashSet是由HashMap生成。 public HashSet() { map = new HashMap<E,
简介 Map和Set是比较常用的两种数据结构。我们在平常的编程中经常会用到他们。只是他们的内部实现机
简介 Map和Set是比较常用的两种数据结构。我们在平常的编程中经常会用到他们。只是他们的内部实现机
Hashtable类   Hashtable继承Map接口,实现一个key-value映射的哈希表。任何非空(non-null)的对
package container; import java.util.HashMap; import java.util.HashSet; import java.util.Map.E
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号