j collection supplement, Map, hash

1. Collection supplement

/

1.1 Elements added by treeSet must be sorted

Two ways:
1 The class corresponding to the element to be added implements the java.lang.Comparable interface, and implements the compareTo method
2 Use the java.util.Comparator comparator class

If the element to be added meets the two comparators, the Comparator takes precedence (compare method)

Comparable: The element to be added implements this interface and overrides the compareTo method.
Comparator: Comparator classes are often used, such as Integer, in ascending order by default;
what do I want to do in descending order? Use Comparator to sort in descending order

If the class of the added element,
we should use Comparable, because for extension development,
others can also use Comparator to implement new sorting functions

If the class of the added element is not written by us
(1) The class has sorting (implements Comparable) such as Integer but the default sorting result is not what we want.
We can use Comparator to adjust the sorting because the priority is high
(2) If this The class does not implement sorting (Comparable is not implemented); at
this time we need to use Comparator for sorting
because it is impossible to change the source code of other classes

1.2 hash algorithm

A secure encryption algorithm
changes the value of variable length to fixed length value
There is a hash conflict

Hash table: The
linked list is stored in the array (singly linked list). There are four attributes in the linked list node:
1 key 2 value 3 next 4 hash

Hash table is a data structure
encapsulated in HashSet, HashMap and HashTable in the form of encapsulation.
The hashTable is outdated.

Hash algorithm in java refers to the hashCode function and rewriting

The purpose of hashing is to query fast; because hash is a fixed value

The hash process
gets the object,
calls the object's own hashCode() method,
and then performs the hash algorithm to
get the array subscript
and save the value to the corresponding array

Set features: disorder and non-repeatability (hashCode and equals)

HashSet 和 HashMap

HashSet is the encapsulation of HashMap. The
essence is that the
default initial capacity of a HashMap is 16
encapsulation. After the HashSet shields the value value, it
can only operate on the key,
so when you use the set to add, you only need to pass in the key.

Adding process
Use the key in the added key-value pair, call the hashCode method of the key to generate the hash value,
perform the hash algorithm to get the array subscript

判断该下标上是否有元素,如果没有把该键值对 保存到该数组中即可;
													如果该数组中有对象 则调用key的equals方法和数组中的元素进行比较
如果相等则key不添加value值覆盖;								
如果不相等,就把该对象的添加到已有元素的next属性,形成链表;
如果添加的时候就已经是链表了,则需要用key和链表中所有的元素key进行比较是否相等

Because hashCode and equals need to be used in the hash table to represent the uniqueness of the object

So when adding custom types, you
need to consider re-hashCode and equals methods as required

2.Map

2.1Map: Unordered and repeatable

  • Value can be repeated and key cannot be repeated
  • The operations of Map and Set are basically the same
  • Object put(Object key,Object value): Add key-value pairs to the map
  • void clear(): clear
  • int size(): add the number
  • boolean isEmpty(): Determine whether it is empty
  • Object get(Object key): get value according to key
  • Collection values(): Get all the value values ​​in the map and return them as a collection
  • booelan containsKey(Object key): Determine whether to contain a key
  • booelan containsValue(Object value): Determine whether to contain a value
  • Set keySet(): Get all the keys in the map and return it as a Set collection
  • Set entrySet(): Return the key-value pair mapping in the map (key=value)
  •  							以Set集合形式返回
    
  • V remove(Object key): Delete the specified mapping relationship according to the key
  •  										返回value值
    
  • map cannot be traversed directly
  • It can be traversed indirectly through methods such as keySet

2.2SortedMap is an interface

  • The only realization is that the TreeMap elements must be ordered and will be sorted according to certain regulations
  • Reasons for sorting:
  • 1 The added element implements the Comparable interface
  • 2. Write a comparator class, which must implement the Comparator interface
    3. Type checking
    3.1 Type checking: During the
    compilation process, check whether the data type matches
  • What is generic
  • Just like arrays, collections can only hold the same data type
  • After the introduction of generics, we can specify the type of storage
  • During the compilation phase, the compiler will check whether the type of the added data matches
  • Generics can only refer to data bureau types
  • Advantages: unified data types
  •  	减少数据类型转换
    
  • Disadvantages: Only a single type of element can be stored
  • The data type of the collection is specified when the collection is declared
  • After specifying the type, when adding data to the collection
  • The compiler will check the data type

Guess you like

Origin blog.csdn.net/MIRACLE_Ying/article/details/113101302