Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HashSet in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use HashSet in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use HashSet in Java.

1. The difference between HashSet and TreeSet

The underlying HashSet uses the Hash table.

The principle of ensuring the uniqueness of an element: determine whether the hashCode value of the element is the same. If it is the same, it continues to determine whether the element's equals method is true.

Red and black trees are used at the bottom of TreeSet.

Make sure that the uniqueness of the element is achieved through the Comparable or Comparator interface.

2. The difference between HashSet and HashMap

In fact, the underlying implementation of HashSet is still HashMap, but it only uses Key, as follows:

(1) at the bottom of HashSet's add method, use HashMap's put method to build key=e,value=PRESENT into key-value key-value pairs. When e exists in HashMap's key, value will overwrite the original value, but key will remain unchanged, so if you add an existing e element in HashSet, the newly added element will not exist in HashMap, so this satisfies the feature that elements in HashSet will not be repeated.

(2) the contains method of HashSet uses HashMap to get the containsKey method.

Knowledge point expansion:

Elements in HashSet are actually objects, and some common basic types can use its wrapper class.

Basic type reference type booleanBooleanbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacter

The HashSet class is located in the java.util package and needs to be introduced before use. The syntax format is as follows:

Import java.util.HashSet; / / introduces the HashSet class

For the following example, we create a HashSet object, sites, to hold string elements:

HashSet sites = new HashSet (); at this point, I believe you have a better understanding of "how to use HashSet in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report