1 The difference between HashMap and HashSet is the most frequently asked question in the Java interview. If not involved in the collection framework and multi-threaded interview, can be said to be incomplete. The problem of collection framework is not related to HashSet and hashmap, it can be said to be incomplete. Both HashMap and HashSet are part of the collection framework, which allows us to work with collections of objects. Collection framework has its own interface and implementation, mainly divided into set interface, list interface and queue interface. They have their own characteristics, the set of the set does not allow the object has a duplicate value, list allows to repeat, it is the collection of objects indexed, the queue works as FCFS algorithm (first Come, first Serve). 2 3 first let's look at what is HashMap and hashset, and then compare the differences between them. 4 What is HashSet5 6 HashSet implements the set interface, which does not allow duplicate values in the collection, when we refer to HashSet, the first thing is to ensure that the object overrides the Equals () and the Hashcode () method before storing the object in HashSet. This allows you to compare the values of objects to ensure that there are no equal objects stored in the set. If we do not override both methods, the default implementation of this method will be used. 7 8 Public BooleanThe Add (Object O) method adds an element to the set, returns false immediately when the element value is duplicated, and returns True if added successfully. 9 What is HashMapTen OneThe HASHMAP implements the map interface, and the map interface maps the key-value pairs. Duplicate keys are not allowed in map. The map interface has two basic implementations, HashMap and TreeMap. TreeMap preserves the order in which objects are arranged, while HashMap does not. HashMap allow keys and values to be null. HashMap is non-synchronized, but the collection framework provides a way to guarantee hashmapsynchronized, so that when multiple threads access HashMap at the same time, only one thread is guaranteed to change the map. A - PublicThe object put (object Key,object value) method is used to add elements to the map. - the You can read this article to see how HashMap works, and this article looks at the difference between HashMap and Hashtable. - the difference between HashSet and HashMap -*hashmap* *hashset* - HashMap implements the map interface HashSet implements the set interface + HashMap Store key value pairs hashset only store objects - Use the put () method to place an element into a map using the Add () method to place the element in the set + using the Key object in HashMap to calculate the Hashcode value HashSet uses the member object to calculate the Hashcode value, hashcode may be the same for two objects, so the Equals () method is used to determine the equality of the object, and if two objects are different, Then return false AHashMap is faster because it is more slow to use a unique key to get an object hashset than HashMap
The difference between HashMap and HashSet