Map interface and implementation class in Java

Get into the habit of writing together! This is the 17th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Map interface and implementation class in Java

Required: Master the characteristics of the Map interface and its implementation classes and the usage of related methods.

The main functions of the Map collection:

1: Add features

       V put(K key, V value): add element.

              If the key is stored for the first time, store the element directly and return null

       If the key is not stored for the first time, replace the previous value with the value and return the previous value

2: delete function

        void clear(): removes all key-value pair elements

       V remove(Object key): delete the key-value pair element according to the key, and return the value

3: Judgment function

       boolean containsKey(Object key): Determines whether the collection contains the specified key

       boolean containsValue(Object value): Determines whether the collection contains the specified value

       boolean isEmpty(): Determine if the collection is empty

4: Get function

       Set<Map.Entry<K,V>> entrySet():???

       V get(Object key): Get the value according to the key

       Set<K> keySet(): Get the set of all keys in the set

       Collection<V> values(): Gets a collection of all values ​​in the collection

5: Length function

       int size(): Returns the logarithm of key-value pairs in the collection

The way to traverse the elements of the Map collection:

Ideas:

(1):

       A: Get all keys

       B: Traverse the set of keys and get each key

       C: Find the value according to the key

(2):

       A: Get a collection of all key-value pair objects

       B: Traverse the collection of key-value pair objects to get each key-value pair object

       C: Get key and value from key-value pair object

HashMap collection:

HashMap: is the implementation of the Map interface based on the hash table.

The role of the hash table is to ensure the uniqueness of the keys.

HashMap<String,String>:键:String        值:String

Observe the addition of key-value duplicate objects: coverage

The HashMap collection stores custom objects and guarantees the uniqueness of elements:

HashMap<c1,String>: key: c1 value: String

       Requirement: Two objects are the same object if their member variable values ​​are the same.

Override equals() and hashCode()

TreeMap collection:

TreeMap: is an implementation of the Map interface based on red-black trees.

HashMap<String,String>:键:String       值:String

Natural sorting:

       Comparable interface provides only one method: compareTo(Object obj) , the return value of this method is int. If the return value is positive, it means that the current object (the object calling this method) is "bigger" than the obj object; otherwise, it is "smaller"; if it is zero, it means that the two objects are equal.

       When using the TreeSet natural sorting method, you need to implement the Comparable interface, annotate the generic type as the current object class, and then override the compareTo() method.

       How to use the compareTo method:

@override

public int compareTo(类名 形参){

    return 形参.返回int类型的方法 - this.与前面相同的方法;

    //从大到小排序。若从小到大只需交换减数与被减数

}
复制代码

Comparator sort:

       Important: When using the TreeSet comparator sorting method, you do not need to implement the Comparable interface, only use the anonymous inner class to override the compareTo method when creating a new TreeSet. This method can be used when the object class is a Java inner class or other classes that do not have permission to modify. The effect is the same as natural sorting. When overriding the compareTo method, pay attention to the order in which the two parameter properties are subtracted.

   c1 = new TreeSet (new Comparator(){
        //使用匿名内部类
        @Override
        public int compare(c1 o1, c1 o2) {
            return o1.get() - o2.get();
        }
    });

复制代码

Guess you like

Origin juejin.im/post/7087407778343419918