Data Structures with java-table (1)

xiaoxiao2021-03-06  43

Yesterday, I realized the MVC's small program. When I was modeling Javabean, I used Hashmap. I saw it when I went back. I was not difficult. The impression is a bit like SQL. I think of it, I suddenly feel my horizontal thinking ratio logic. Thinking is stronger, so maybe it is bad.

Table is a container data structure, a good class ratio is the word in the dictionary and the detailed explanation of it. The table is a sequence sequence, the first portion of the context is a KEY, an index, which is equivalent to the subscript in the array. The second part is that the corresponding value.Table is also called the associated array, and can be implemented in two parallel arrays, a saved key, a saved value. Where Java.util's MAP interface defines 4 implementations (Not JDK5.0 Abstractmap, HashMap, Treemap, Weakhashmap.

Map map = new hashmap ();

Map.Put ("a", "day");

Map.Put ("B", "Month");

Map.put ("c", "year");

Map.put ("d", "min");

Map.Put ("e", "second");

Map.Put ("f", "hour");

System.out.println ("MAP = / T" MAP); // Tostring () Method.

System.out.println ("Map.size = / T" map.size ());

System.out.println ("Map.keyset = / T" map.keyset ());

System.out.println ("Map.Values ​​= / T" map.values ​​());

System.out.println ("Map.get (/" c / ") = / t" map.get ("c"));

System.out.println ("Map.Remore (/") = / t " map.remove (" c "));

System.out.println ("MAP = / T" MAP);

System.out.println ("Map.size = / T" map.size ());

Run Result:

Map = {d = min, a = day, c = year, f = hour, b = month, e = second}

Map.size = 6map.keyset = [d, A, c, f, b, e]

Map.values ​​= [Min, Day, Year, Hour, Month, SECOND]

Map.get ("c") = year

Map.remore ("c") = year

Map = {d = min, a = day, f = Hour, b = month, e = second}

Map.size = 5

The Key / Value storage order in Hashmap depends on Table's capacity and the Hashcode of these objects.

Public static void main (string [] args)

{

Printhashcode ("ABC");

Printhashcode ("DEF");

Printhashcode ("GHI");

Printhashcode ("jkl");

Printhashcode ("abc");

Public Static Void PrinThashcode (String Word)

{

System.out.println (Word ":" Word.hashcode ());

}

Run Result:

ABC: 96354

DEF: 99333

GHI: 102312

JKL: 105291

ABC: 96354

Hash table is a type, which uses a special function to calculate the data value from Key to determine the storage location, this special function is called the Hash function.

Private static final int mask = 0x7fffffff; // 2 ^ 32-1

Private static final int capacity = 11; // Capacity IS 11

Public hashcode ()

{

}

Public static void main (string [] args)

{

PRINTHASHCODE ("OHR");

Printhashcode ("TOR");

Printhashcode ("HUT");

Printhashcode ("RAD");

Printhashcode ("tag");

Printhashcode ("UHR");

}

Public Static Void PrinThashcode (String Word)

{

System.out.println (Word ":" Hash (Word));

}

Public static int hash (String Object)

{

Return (Object.hashcode () & mask)% Capacity;

}

Run Result:

OHR: 73

Tor: 45

HUT: 13

RAD: 99

TAG: 4

UHR: 82

Here, (Object.hashcode () & mask)% Capacity;

Private static final int mask = 0x7fffffff; // 2 ^ 32-1

Private static final int capacity = 11; // Capacity IS 11

Capacity IS 11, MASK = 2 ^ 32-1 = 2 147 483 647Object.hashcode () & mask is removed, otherwise, the loss is also negative after returns. The result must be between 0 and 10.

RAD: 3

TAG: 3

Conflict, the most common method is:

Place the tag in the end (the program example problem, the ABC in the above program is also changed here). The name of this conflict elimination algorithm is: linear detection algorithm

If CAPACITY is changed to 101, the result of Return is 0 ~ 100, and the order after we establish a HashMap object (change a procedure), I tested it, but I saw the result, but at least the principle ( May be a different reason for the JDK version?)

If you store 6 elements with a Hash Table for a capacity of 10, performance can be, but if we use 6 access?

Measuring the degree of congestion of Hash Table, load factor:

Practical elements / Capacity

If it is actually equal to capacity, the coefficient is 100%, if the coefficient is greater than capacity, then?

The default value of the capacity is 100, the default load factor is 75%. If the load factor is exceeded, it increases its capacity.

Hashmap (int initialcapacity, float loadingfactor)

The principle of linear detection is the principle of linear detection. When an element is locked by the Hash function to a location already used, the algorithm will be incremented by a null location, if it goes to the HASH table Finally, jump to the initial position.

Square detection

Public static int hash (String Object)

{

size;

INT H = (Object.hashcode () & mask)% Capacity;

INT jump = 1;

While (used [h])

{

INT g = H;

System.out.print (h ",");

H = (g jump * jump)% Capacity;

JUMP ;

} Many elements will be skipped during the detection, the weight is relatively uniform. It is relatively high in performance.

Independent chain:

Avoid conflicts, allowing HASH to place multiple elements per vacancy, this method uses a linked list to store multiple elements, and the vacancy of the table is called a hanging bucket.

Java.util.hashmap class is the opposite chain.

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

New Post(0)