Home > Java > javaTutorial > What are the usage characteristics of HashSet in java

What are the usage characteristics of HashSet in java

PHPz
Release: 2023-04-15 09:52:02
forward
1295 people have browsed it

The difference between HashSet and TreeSet

1. The bottom layer of HashSet uses a Hash table. The principle of ensuring the uniqueness of elements: Determine whether the hashCode values ​​of the elements are the same. If they are the same, it will continue to determine whether the equals method of the element is true.

The bottom layer of TreeSet uses a red-black tree.

Ensuring the uniqueness of elements is achieved through the Comparable or Comparator interface.

The difference between HashSet and HashMap

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

(1) At the bottom of the add method of HashSet, use the put method of HashMap to construct key=e, value=PRESENT into a key-value pair. When e exists in the key of HashMap, the value will overwrite the original value. But the key remains unchanged, so if an already existing e element is added to the HashSet, the newly added element will not exist in the HashMap, so this satisfies the feature that the elements in the HashSet will not be repeated.

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

What are the advantages of Java

1. Simple, you only need to understand the basic concepts to write applications suitable for various situations;

2. Object-oriented ;

3. Distribution, Java is a network-oriented language;

4. Robustness, Java provides automatic garbage collection for memory management, preventing programmers from easily generating errors when managing memory. Error;

5. Security, Java used in network and distributed environments must prevent virus intrusion.

6. Architecture neutral, as long as the Java runtime system is installed, it can run on any processor.

7. Portability, Java can be easily transplanted to different machines on the network.

8. Interpretation and execution, the Java interpreter directly interprets and executes the Java bytecode.

The above is the detailed content of What are the usage characteristics of HashSet in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template