Above: Java programming ideas object set (array, list, set, MAP) 1
22. If you use a self-created class as the "key" of hashmap, you must overwrite both hashcode () and equals (). The reason is as follows:
1). the default hashcode () is inherited from the object. It usesObject addressCalculate the hash code.
Therefore, even if the instance content of the two objects is the same,The object address is different.So the hash code obtained by default hashcode () is different. Example:
Import Java. util. hashmap; import Java. util. map; public class testhashmap {private int ID; Public testhashmap (int id) {This. id = ID;} public void func () {system. out. println (ID);} Public String tostring () {return "testhashcode #" + ID;} public static void main (string [] ARGs) {testhashmap testhashcode = new testhashmap (2012); Map <object, Object> hashmap = new hashmap <object, Object> (); hashmap. put (testhashcode, 2012); If (hashmap. containskey (testhashcode) {system. out. println ("found ID:" + testhashcode. ID + "testhashcode");} else {system. out. println ("the ID cannot be found:" + testhashcode. ID + "testhashcode");} testhashmap testhashcode1 = new testhashmap (2012); If (hashmap. containskey (testhashcode1) {system. out. println ("testhashcode with ID:" + testhashcode1.id + "");} else {system. out. println ("testhashcode with ID:" + testhashcode1.id + "cannot be found");} system. out. println ("--------------------------------"); system. out. println ("testhashcode hash code:" + testhashcode. hashcode (); system. out. println ("testhashcode1 hash code:" + testhashcode1.hashcode ());}}
Running result:
Yes"Find"And"Not found"This embarrassing situation.
2) in this case, overwrite the hashcode () of oject and add the following in testhashmap:
public int hashCode(){ return id;}
Run the command again and the result is as follows:
3 ). this is because hashmap uses equals () to determine whether the current "key" is the same as the "key" in the table. By default, equals () is inherited from the object and the object. equals () is only a comparisonObject address. Then add the following to testhashmap:
Public Boolean equals (Object O) {// o instanceoftesthashmap check whether o is a testhashmap instance return (O instanceof testhashmap) & (ID = (testhashmap) O ). ID );}
Running result:
To sum up, if a custom class does not overwrite hashcode () and equals () for the "key", the data structure in which the hash is used is (hashset, hashmap, linkedhashse, or linkedhashmap).
23. The value of hash lies in speed: Hash enables fast query. The bottleneck lies inQuery speed of "key", Hash usageArray(Store the fastest data structure of a group of elements) to save the hash key information. The array does not keep the "key" itself, but generates a number through the "key" object and uses it as the small mark of the array. This number isHash code, Defined in the object. And may be overwritten by your classHashcode ()Generate. To solve the problem that the array capacity is fixed, different "keys" can generate the same subscript and conflict with each other.
24. query a "value": Calculate the hash code and use the hash code to query the array. If a conflict occurs, it is processed by an "external link". In this case, the array does not directly save the "value", but stores the list of "value. Then use the "value" in the listEquals ()Method for linear query.
25. The underlying layer of arraylist is supported by arrays, while the lateral list is implemented by a bidirectional linked list. Therefore, if you often want to insert or delete elements in a table, the sorted list is more suitable (the sorted list also has other functions based on abstractsequentiallist); otherwise, you should use the faster arraylist.
26. Tree behavior: it is always in the sorting state. Treeset and hashmap are all sorted.
27. The list or array must be sorted only before binarysearch () is executed.
28. Summary:
1 ).
Array associates numbers with objects. It stores objects with clear types. You do not need to convert the result type when querying objects. It can be multidimensional and can save basic data types. However, once an array is generated, its capacity cannot be changed.
2). Collection stores a single element, while MAP stores the associated key-value pairs.
3). Like arrays, list also establishes associations between numbers and objects. It can be considered that arrays and lists are sorted containers. List can automatically expand the capacity. But listThe basic type cannot be saved.Only object references can be saved. ThereforeType conversion must be performed on the results of the objects retrieved from the container.(Rtti ).
4). If you want to perform a large number of random access, use arraylist. If you want to insert or delete elements from the list frequently, use explain list.
5 ).
Queue, bidirectional queue, and stack behavior are supported by the queue list.
6). Map is a design that associates objects with objects. Hashmap focuses on fast access. treemap keeps the "key" in the sorting state, so there is no hashmap fast. Linkedhashmap can be used to keep the element insertion sequence.LRU ("least recently used") algorithm for its re-sorting.
7). Set does not accept repeated elements. Hashset provides the fastest query speed, and treeset keeps elements in order. Linkedhashset stores elements in the insert sequence.
8) the outdated vector, hashtable, and stack should not be used in the new program.
The above content is compiled from Java programming ideas. If any omission exists, please leave it blank!