The difference between Java---hashset and hashmap

Source: Internet
Author: User

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).

First let's look at what is HashMap and hashset, and then compare the differences between them.

What is HashSet

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.

The public boolean add (Object O) method is used to add elements to the set, which returns false immediately when the element value repeats, and returns True if added successfully.

What is HashMap

The 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 hashmap synchronized, so that when multiple threads access HashMap at the same time, only one thread can change the map.

The public object put (object Key,object value) method is used to add elements to the map.

You can see how HashMap works and see 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 put elements into a map Use the Add () method to put an element into a set
Use Key object in HashMap to calculate 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, return false
HashMap is faster because it uses a unique key to get the object HashSet more slowly than HashMap.

If you know any other differences, please leave a message.

Original link: javarevisited translation: importnew.com-Tangxiaojuan

The difference between Java---hashset and hashmap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.