Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java class HashSet     - Share Java-based multithreading file case (Programming)

- Ubuntu 14.04 users how to install VLC 2.2.0 (Linux)

- CentOS 6.4 compiler installed MySQL 5.6.14 (Database)

- Oracle 11g RMAN cross-platform transfer table space (Database)

- floating IP in OpenStack neutron (Server)

- Python programming style (Programming)

- Linux memory Cache Analysis (Linux)

- Broadcom transplanted to OpenWrt summary (Programming)

- jdbc Oracle database connection string writing pluggable (Database)

- Use HugePages optimize memory performance (Database)

- Linux Getting Started tutorial: Experience KVM Virtual Machine chapter (Linux)

- Bash common several configuration files (Linux)

- Lua4.0 interpreter documents (Programming)

- Good wireless network security information spread in the air (Linux)

- Json Applications of FastJson (Programming)

- RPM package management under Linux (Linux)

- RedHat6.4 installation tutorial --- Minimal Edition (Linux)

- 10 useful Linux command line tips (Linux)

- Laravel cache paged results (Server)

- Android design patterns - state mode (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.
     
         
       
         
  More:      
 
- Handle large data problems Bit-map method (Programming)
- Using Oracle for Oracle GoldenGate to achieve a one-way data synchronization (Database)
- EChart simple and practical control on chart (Programming)
- Detailed driver compiled into the Linux kernel (Programming)
- Mac OS X Server installation and application (Linux)
- DRBD switchover (Server)
- OpenWrt modify flash size (Linux)
- Ten best plug surge Emacs Productivity (Linux)
- Sniffer install applications in Linux supplement (Linux)
- Apache Spark1.1.0 deployment and development environment to build (Server)
- Oracle 11g RMAN cross-platform transfer table space (Database)
- Ubuntu Apache virtual host configuration (Server)
- MySQL Online DDL tools of pt-online-schema-change (Database)
- Docker in the development and practice of IFTTT (Server)
- Shell Programming points to note about the function (Programming)
- CentOS 6.6 source compiler GCC upgrade to 4.8.2 (Linux)
- Grading defense against Linux server attacks (Linux)
- Oracle database, some basic grammatical structures (Database)
- CentOS7 installation configuration Redis-3.0.0 (Database)
- Linux RPM (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.