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 :
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.
相关推荐
主要介绍了Java中HashSet和HashMap的区别_动力节点Java学院整理,需要的朋友可以参考下
自己写的例子,关于HashSet遍历和HashMap遍历的. 感谢大家参考
treemap treeset hashset hashmap 简要介绍
什么是HashSet? HashSet实现了Set接口,它不允许集合中有重复的值,当我们提到HashSet时,第一件事情就是在将对象存储在HashSet之前,要先确保对象重写equals()和hashCode()方法,这样才能比较对象的值是否相等,以...
之所以把HashSet和HashMap放在一起讲解,是因为二者在Java里有着相同的实现,前者仅仅是对后者做了一层包装,也是说HashSet里面有一个HashMap(适配器模式)。因此本文将重点分析HashMap。 HashMap实现了Map...
编写一个查询类,输入学生学号,若该生在数据文件(test.txt)中存在,在JTextArea中显示该生信息 若该生在数据文件(test.txt)中不存在,显示”查无此人”,可反复查找.在输出中,能显示该生的总成绩和平均成绩,将显示结果...
Java不同数据存储类型使用不同遍历方法效率研究 GitHub代码仓库 数据存储类型 ArrayList HashSet HashMap LinkedList 遍历方法 传统遍历方法 for(int i=0;i<list.size();i++) { String str = list.get(i); ... ...
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSet 的源代码,可以看到如下代码:
HashMap、HashTable和HashSet是Java中常用的数据结构,它们的底层实现原理以及区别如下:HashMap底层实现原理: HashMap基于哈希表(HashTable)实现,它通过散列算法将键值对映射到数组中。在HashMap中,每个键值对...
HashSet的实现原理 ,HashSet与HashMap的区别 以及 HashSet的底层实现方式
HashTable不支持空键值对! 而HashMap支持空键值对!
数据结构 简单的数据结构,包括HashSet,HashMap,Heap,Red-Black Tree等。
主要介绍了从源码的角度浅析HashMap、TreeMap元素的存储和获取元素的逻辑;从Map与Set之间的关系浅析常用的Set中元素的存储和判断是否重复的逻辑,需要的朋友可以参考下
java HashSet 集合排序,需要通过利用TreeSet集合排序。2013-10-30。
HashMap和HashSet是Java Collection Framework的两个重要成员,其中HashMap是Map接口的常用实现类,HashSet是Set接口的常用实现类。虽然HashMap和HashSet实现的接口规范不同,但它们底层的Hash存储机制完全一样,...
在Java的集合框架中,HashSet,HashMap是用的比较多的一种,顺序结构的ArrayList、LinkedList这种也比较多,而像那几个线程同步的容器用的比较少,像Vector和HashTable,因为这两个线程同步的容器已经不被JDK推荐...
Java Generics and Collections 英文版,详细描述java 泛型技术
本资源包含java集合(hashset treeset hashmap和treemap)的使用实例