The difference between HashMap and HashSet is the most frequently asked question in Java interviews. If the Collection framework and multi-threaded interview are not involved, it can be said that it is incomplete. The problem with the Collection framework does not involve HashSet and HashMap, and it can be said to be incomplete. HashMap and HashSet are both part of the collection framework, which allow us to use collections of objects. The collection framework has its own interface and implementation, which are mainly divided into Set interface, List interface and Queue interface. They have their own characteristics. The set does not allow objects to have duplicate values, and the List allows duplicates. It indexes the objects in the set. The working principle of Queue is the FCFS algorithm (First Come, First Server).
First let's look at what HashMap and HashSet are, and then compare the differences between them.
What is HashSet
HashSet implements the Set interface, which does not allow duplicate values in the set. When we mention HashSet, the first thing is to make sure that the object overrides the equals() and hashCode() methods before storing the object in the HashSet, so that the object can be compared whether the values of the object are equal, so as to ensure that there are no equal objects stored in the set. If we do not rewrite these two methods, the default implementation of this method will be used.
The public boolean add(Object o) method is used to add elements in the Set. When the element value is repeated, it will immediately return false, and if it is successfully added, it will return true.
What is HashMap
HashMap implements the Map interface, which maps key-value pairs. Duplicate keys are not allowed in the Map. There are two basic implementations of the Map interface, HashMap and TreeMap. TreeMap saves the order of objects, while HashMap cannot. HashMap allows keys and values to be null. HashMap is non-synchronized, but the collection framework provides methods to ensure that HashMap synchronized, so that when multiple threads access HashMap at the same time, it can ensure that only one thread changes the map.
The public Object put(Object Key,Object value) method is used to add elements to the map.
You can read this article to see how HashMap works, and this article to see how HashMap and HashTable are different.
The difference between HashSet and HashMap
| *HashMap* | *HashSet* |
| HashMap implements Map interface | HashSet implements Set interface |
| HashMap to store key-value pairs | HashSet only stores objects |
| Use put() method to put elements into map | Use the add() method to put elements into set |
| HashMap uses key objects to calculate hashcode values | HashSet uses member objects to calculate hashcode values. The hashcode may be the same for two objects, so the equals() method is used to judge the equality of objects. If the two objects are different, then return false. |
| HashMap is faster because it uses the only key to get the object | HashSet is slower than HashMap |
If you know any other differences, please leave a message.
Original link: Javarevisited Translation: ImportNew.com - Tang Xiaojuan Translation Link: http://www.importnew.com/6931.html