Hashcode base Hashcode in Java, represents a feature of an object. Different object hash codes are different. Hash code is a data structure algorithm, in Java, the common hash code algorithm obtains the hash code specific value is how to obtain it? To analyze: 1. Hashcode in the string class is obtained from a string contained in the string class, and a hash code is obtained based on the hash algorithm, and my hash code is the same as long as my character content is equal. eg[Java]View PlainCopyprint?
- Public static void Main (string[] args) {
- String a ="CX";
- String B = "CX";
- System.out.println (B.hashcode () ==a.hashcode ());
- }
I think we've already guessed it, true!. The hashcode in the 2.Integer class is the same as the string, and is the hash code generated based on the values contained in the class. Two integer values of the same size, then its hashcode is equal. eg[Java]View PlainCopyprint?
- Integer a =11;
- Integer B = 11;
- System.out.println (B.hashcode () ==a.hashcode ());
The result is still true!. Hashcode in the 3.Object class is not the same as before, he is the hash algorithm based on the memory address of the object after hashing, because the memory address of each object is not the same, so the hashcode is different. We often use hashcode and equals to judge whether an object is equal! In Hashtable, HashMap, HashSet, Linkedhashmap, we're going to tell if we have the same object in the collection, we're going to rewrite the object's hashcode and equals to help us get to the same object in the collection! In the above set of hash usage, the Hashcode method of the object is executed automatically when the operation is performed. To determine if it is the same object. * * If the hashmap is not the same at the time of the put, then when I add the object to the key will be called hashcode and the Equals method, to determine whether the same object, if not then join the collection, if not, then do not join. So we rewrite Hashcode, can return an object of Uuid.hashcode () so to Judge HashSet is the same, the principle is the same.[Java]View PlainCopyprint?
- Map<statictest, string> map = new hashmap<statictest, string> ();
- Statictest st = New Statictest ();
- St.seti (55);
- St.setname ("CX");
- Map.put (St, "OK");
- set<statictest> set = Map.keyset ();
- Statictest obj = new Statictest ();
- Obj.seti (55);
- Obj.setname ("cx1");
- Map.put (obj, "OK");
- For (Statictest statictest:set) {
- System.out.println (Statictest.getname ());
- }
- }
Overridden by Hashcode () and Equals ():[Java]View PlainCopyprint?
- @Override
- public int Hashcode () {
- System.out.println ("Executive Hashcode");
- return I.hashcode ();
- }
- @Override
- Public Boolean equals (Object obj) {
- System.out.println ("equals");
- if (this.getclass () = = Obj.getclass ()) {
- return I.equals (((statictest) obj). Geti ());
- }
- return false;
- }
The result of the final output is Cx,map, which is the first time I added the object. The second addition because I is the same, so I judge that they are the same object, it is not put in the!!!! This article turns from http://blog.csdn.net/qq_25223941/article/details/49905941, thanks to the original author
Java's hashcode is used to determine if an object is equal