Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java class HashSet     - Can not remember how to solve the problem under Ubuntu brightness setting (Linux)

- Forwarding module with Apache reverse proxy server (Server)

- C ++ 11 smart pointers (Programming)

- pkg-config to use (Linux)

- To install Spotify in Ubuntu / Mint (Linux)

- Binary tree traversal: the first sequence in order preorder recursive and non-recursive and traversal sequence (Programming)

- HomeKit User Interface Guidelines (Linux)

- RedHat Linux 7 build yum source server (Server)

- Java to achieve local fileCopy (Programming)

- imp / exp Oracle Database import and export commands (Database)

- Oracle 11g through SCN do incremental backup repair standby library detailed process (Database)

- How to install GIMP 2.8.16 in Ubuntu 16.04,15.10,14.04 (Linux)

- Expert advice: Do not use the computer security IE browser (Linux)

- MySQL performance comparison of large amounts of data storage (Database)

- Installed in the desktop version of Ubuntu Unity Tweak Tool (Linux)

- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)

- PostgreSQL log classification and management (Database)

- Linux file content inspection - cat, tac, no, more, less, head, tail, od (Linux)

- MySQL binlog automatic cleanup script (Database)

- What is Java EE (Programming)

  Java class HashSet
  Add Date : 2017-08-31      
  If you want to find a collection contains a certain object, you need this object and this set of each object in turn to compare and judge, until you find the object, or until all the objects are relatively far once (if the last an object is an object you are looking for, or the collection does not contain the object you want to find). When the number of objects in the collection are more efficiency is very low. To improve efficiency, Hash algorithm is proposed. Hash algorithm for each object code to calculate a Hash, Hash code assigns an object to a storage area in accordance with, for example, contains a collection of a lot of people, based on nationality, the Chinese people is a storage area, a storage area Americans British is a storage area, ....... So if you want to find out whether the collection has a Chinese people, Chinese people went to the storage area to compare on the line, thereby greatly enhancing efficiency.

Java implemented Hash collection is HashSet. When HashSet find an object, first with hashCode () method to calculate the object's Hash code, then according to Hash code to the appropriate storage area with equals () method to find, thus increasing efficiency. Because it is set, so the same object can have only one.

hashSet example is shown below:

package my;

import java.util.HashSet;
import java.util.Set;

class Person {
    // Sex
    String sex;
    // Name
    String name;
    // Height
    Double hei;
    // Weight
    Double wei;
    public Person (String n, String s, Double h, Double w) {
        this.name = n;
        this.sex = s;
        this.hei = h;
        this.wei = w;
    public String toString () {
        return "\ n Name:" + this.name + "Gender:" + this.sex + "Height:" + this.hei + "Weight:" + this.wei;

public class myHS {

    private static Set < Person> mySet = new HashSet < Person> ();
    public static void main (String [] args) {
        mySet.add (new Person ( "Tom", "Male", 170.0,70.0));
        mySet.add (new Person ( "Peter", "Male", 175.0,70.0));
        mySet.add (new Person ( "Kate", "Female", 168.0,60.0));
        mySet.add (new Person ( "Alice", "Female", 161.0,55.0));
        mySet.add (new Person ( "Jack", "Male", 190.0,95.0));
        mySet.add (new Person ( "Jack", "Male", 190.0,95.0));
        System.out.println (mySet);


The above example first defines the Person class, and then define a HashSet, and added 5 Person to the collection, in which one person was added twice, the results are as follows:

Jack is seen the same person, but it appears twice in the collection, which is what causes it? This is because, Person is a subclass of Object, and Object class equals () method is based on the object's memory address to determine whether two objects are equal, due to the insertion of two Jack memory address is certainly not the same, so the judge the result is not equal, so the two are inserted. So, we need to override the equals () method to determine if two objects are the same object.

    // Override equals method
    public boolean equals (Object obj) {
        // Address equal, certainly the same object
        if (this == obj) {
            return true;
        // Types are different, certainly not in the same class object
        if (! (obj instanceof Person)) {
            return false;
        // Same type, downcast
        Person per = (Person) obj;
        // If two objects of the same name and gender, it is the same person
        if (this.name.equals (per.name) && this.sex.equals (per.sex))
            return true;
        return false;

Jack still visible was inserted twice, which is what causes it? This is because the Hash Object code is returned Hash object address, and the two objects Hash address is certainly not equal, so the object is inserted 6 times in six storage area, equals () method no storage operation. So, we need to override hashCode () method, according to the name of the object to calculate the Hash code.

// Override hashCode method

public int hashCode () {

return this.name.hashCode ();


Visible, Jack inserted only once, and finally correct. If calculated according to sex objects Hash code, the result is correct, Jack will only be inserted once. However, if the two objects of different gender, as follows:

mySet.add (new Person ( "Jack", "Male", 190.0,95.0));

mySet.add (new Person ( "Jack", "Female", 190.0,95.0));

This is because although the two objects of the same Hash code (either by name or by gender to calculate, Hash codes are the same), but the equals () method to determine which two objects are not equal, so are inserted.
- PHP call a Python program (Programming)
- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)
- Linux yum command Detailed (Linux)
- Hadoop 2.6.0 standalone configuration and pseudo-distributed configuration under Ubuntu 14.04 (Server)
- Oracle rebuild index script (Database)
- Distributed Firewall Design on Linux platform (Linux)
- SUSE Linux network configuration and firewall configuration (Linux)
- Ubuntu install Scala 2.10.x version (Linux)
- Java 8 stream parsed into SQL (Programming)
- Three easy open source firewall on the Linux platform (Linux)
- Ubuntu install Geary (Linux)
- Ubuntu to install systems Indicator Sticky Notes 0.4.4 (Linux)
- Java Graphics2D Paint (Programming)
- Linux, grep, sed usage (Linux)
- Linux, modify / retrieve the root password (Linux)
- Linux tool curl and wget advanced use (Linux)
- 10 tips daily Docker (Server)
- Unable to start the network after restart clone a virtual machine (Linux)
- Create and modify Oracle temporary table space (Database)
- Linux Regular expressions grep and egrep (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.