Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java class HashSet     - After restarting network services, DNS address failure (Linux)

- Java singleton mode (Singleton mode) (Programming)

- CKEditor + SWFUpload achieve a more powerful editor (Linux)

- Linux shell scripts bubble sort (Programming)

- Memcached installation, configuration and monitoring (Server)

- 10 Nginx safety tips (Linux)

- MongoDB 3.2 Cluster Setup (Database)

- Linux System Getting Started Learning: Debian download, installation and graphical interface (Linux)

- Graphic Git (Linux)

- Python system default encoding (Programming)

- ImageMagick Tutorial: How to cut images in Linux command line (Linux)

- Linux file permissions chmod chown (Linux)

- Linux see whether there is a hacker program (Linux)

- Single-node Hadoop installation notes distributed pseudo & (Server)

- Docker commonly used commands Description (Linux)

- Ubuntu installed Gimp 2.6.11 (stable version) with PPA (Linux)

- Installation and Configuration OpenVPN server and client on Ubuntu 15.04 (Server)

- Ubuntu Locale configuration problem solving Can not set LC_CTYPE (Linux)

- How to Set Free SSH password on CentOS / RHEL (Linux)

- Difference between TCP and UDP protocols (Linux)

 
         
  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.
     
         
         
         
  More:      
 
- VMware virtual machine to install CentOS 7 (Linux)
- ActiveMQ-based shared file system HA solutions (Server)
- Btrfs file system creation and their characteristics in Linux (Linux)
- C language macro definition #define Usage (Programming)
- CentOS install video converter FFmpeg and cutting tools segmenter (Linux)
- Upgrading to Debian 7.6 glibc 2.15 (Linux)
- awk Programming Model (Programming)
- MySQL 5.7 can not log in problem (Database)
- Why not use the ifconfig command under RedHat Linux 5 (Linux)
- curl Usage: Being the first site header and status codes (Server)
- Use PuTTY key authentication mechanism for remote login Linux (Linux)
- Some common Linux commands Study Notes (Linux)
- CentOS environment prepared crontab scheduled tasks (Linux)
- How to configure MariaDB replication in CentOS Linux (Database)
- Oracle 12C modify spfile path (Database)
- Linux Timing task Crontab command Detailed (Linux)
- Use 3G wireless network card under Linux (Linux)
- Spark SQL job of a lifetime (Server)
- Objective-C basic program structure (Programming)
- Linux file and directory management - ls, cp, mv (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.