Java Programming Ideology (2nd) Learning Notes (9) -3

zhaozj2021-02-17  45

I. Some other discussion of HashMap

1. Using the KEY value in HashMap

1.1. When the Java's library function is used as a KEY value of HashMap, it can be used directly.

Import java.util. *;

Class counter {

INT i = 1;

Public string toString () {

Return Integer.toString (i);

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

Hashmap hm = new hashmap ();

For (int i = 0; i <10000; i )

{

// Hashmap's key type is Integer

Integer r = new integer ((int) (math.random () * 20);

IF (hm.containskey (r))

(Counter) hm.get (r)). I ;

Else

HM.PUT (R, New Counter ());

}

System.out.println (HM);

}

}

1.2. If you use the classes you write in Hashmap as Key, you must override HashCode () and Equals ().

The following code is made as Key with its own Class, but did not overwrite HashCode () and Equals ().

Import java.util. *;

Class groundhog {

INT GHNUMBER;

Groundhog (int N) {ghnumber = n;

Public string toString () {

Return "GROUNDHOG @" Ghnumber;

}

}

Class prediction {

Boolean shadow = math.random ()> 0.5;

Public string toString () {

IF (Shadow)

Return "Six More Weeks of Winter! / N";

Else

Return "Early Spring! / N";

}

}

Public class test {

Public static void main (String [] args) {

Hashmap hm = new hashmap ();

For (int i = 1; i <10; i )

HM.PUT (New Groundhog (i), new prediction ());

System.out.println ("hm =" hm);

System.out.println ("Looking Up Prediction For GROUNDHOG # 3:");

Groundhog GH = New Groundhog (3);

IF (hm.containskey (gh)) // (1)

System.out.Println (PREDiction) HM.Get (GH));

Else

System.out.println ("Key NOT Found:" GH);

}

}

operation result:

HM = {GROUNDHOG @ 9 = Early Spring!

, GROUNDHOG @ 8 = Six More Weeks of Winter!

, GROUNDHOG @ 7 = Six More Weeks of Winter!

, GROUNDHOG @ 6 = Early Spring !,, Groundhog @ 5 = Early Spring!

, GROUNDHOG @ 4 = Early Spring!

, GROUNDHOG @ 3 = Early Spring!

, GROUNDHOG @ 2 = Early Spring!

, GROUNDHOG @ 1 = Six More Weeks of Winter!

}

Looking Up Prediction For Groundhog # 3:

Key Not Found: Groundhog @ 3

Key did not overwrite HashCode () and equals (), then when you get Hash Code through Key, you will get the memory address of the key; The address of Key. So (1) The result of the code comparison is false (because the memory addresses of the two objects are certainly different). Obviously, this is not the result we have to get.

In order to get the correct result, we only need to implement HashCode () and Equals () in a class as a key. : Import java.util. *;

Class groundhog2 {

INT GHNUMBER;

GROUNDHOG2 (INT N) {ghnumber = n;

Public string toString () {

Return "GROUNDHOG2 @" Ghnumber;

}

/ **

* As Ghnumber as Hash Code

* /

Public inthacode () {return ghnumber;}

/ **

* Compare two Key ghnumber values

* /

Public Boolean Equals (Object O)

{

Return (o InstanceOf Groundhog2)

&& (Ghnumber == ((Groundhog2) o) .ghnumber);

}

}

Class prediction {

Boolean shadow = math.random ()> 0.5;

Public string toString () {

IF (Shadow)

Return "Six More Weeks of Winter! / N";

Else

Return "Early Spring! / N";

}

}

Public class test {

Public static void main (String [] args) {

Hashmap hm = new hashmap ();

For (int i = 1; i <10; i )

HM.PUT (New Groundhog2 (i), new prediction ());

System.out.println ("size =" hm.size () ", hm =" hm);

System.out.println ("Looking Up Prediction For GROUNDHOG # 3:");

Groundhog2 GH = New Groundhog2 (2);

IF (hm.containskey (gh))

System.out.Println (PREDiction) HM.Get (GH));

Else

System.out.println ("Key NOT Found:" GH);

}

}

The result of the operation is: hm = {Groundhog2 @ = Early Spring!

, GROUNDHOG2 @ 8 = Six More Weeks of Winter!

, GROUNDHOG2 @ 7 = Six More Weeks of Winter!

, GROUNDHOG2 @ 6 = Six More Weeks of Winter!

, Groundhog2 @ 5 = Early Spring!

, GROUNDHOG2 @ 4 = Early Spring!

, GROUNDHOG2 @ 3 = Six More Weeks of Winter!

, GROUNDHOG2 @ 2 = Early Spring!

, GROUNDHOG2 @ 1 = Early Spring!

}

Looking Up Prediction For Groundhog # 3:

Early Spring!

In the new code, we implemented the HashCode () and Equals () functions in a class as a key, got the desired results.

2. HashMap performance factor

Capacity: Buckets in the table

Initial Capacity: The number of BUCKETs, the initial capacity, and the table.

HashMap and HashSet: Each constructor allows you to specify initial capacity.

Size: Size, all entries in the table.

Load Factor: Load Factor, Size / Capacity (Size / Capacity). The load factor is 0, indicating an empty form, 0.5 is a half full table, and so on. A light load form The chance of collision (Collisions) is relatively low, which is more suitable for the installation and lookup (but will reduce the speed of "through the iterator tour). After the constructor in HashMap and Hashset, the load factor is specified, when the container reaches this load factor, the capacity of the container (BUCKETS number) will be automatically expanded, and the original object is re-imported into new buckets (this Rechashing). HashMap default load factor value is 0.75.

转载请注明原文地址:https://www.9cbs.com/read-29198.html

New Post(0)