当前位置:网站首页>Two column set (map set)

Two column set (map set)

2022-06-21 06:49:00 naoguaziteng

Objective        record

One .Map aggregate

1. summary

2.Map Interface and Collection Different interfaces

3.Map Set system

4. Key correspondence

5.Map Methods in sets

6.Map Traversal of the set

  Two .HashMap aggregate ( Element uniqueness , disorder )

1.HashMap brief introduction

2.HashMap And Hashtable The difference between

3、 ... and .LinkedHashMap aggregate ( Element uniqueness , Orderly )

Four .TreeMap aggregate ( Element uniqueness , Ordered and sortable )


One .Map aggregate

1. summary

         Data for key value correspondence ,Java For our convenience , Gives us another set called Map Data specially storing key value correspondence .Map A set is also called a two column set .Map Interface ----->Map<K,V> Medium K Is the key , The type of key maintained by the mapping ,V yes Index value , The type of the mapping value .

2.Map Interface and Collection Different interfaces

Map Interface is a different from Conllection Interface interface , There is no connection between them . There are also big differences !

  • Map It's a double column ,Collection It's a separate list
  • Map The key is unique ,Collection The subsystem of Set Is the only one.
  • Map The data structure of the collection is valid for keys , Independent of value ;Collection The data structure of a collection is valid for elements

3.Map Set system

4. Key correspondence

A key can only correspond to one value , Same key , It's worth overriding . And the data structure of all double column sets is only related to the key , It has nothing to do with value .

import java.util.HashMap;

public class MapTest {
    public static void main(String[] args) {
        HashMap<Integer, String> hm=new HashMap();
        hm.put(1001," Zhang San ");
        hm.put(1002," Li Si ");
        hm.put(1003," Wang Wu ");
        hm.put(1001," Liu Liu ");
        System.out.println(hm.get(1001));   // Liu Liu 
    }
}

5.Map Methods in sets

  • put();      Add key values to the collection ( This actually has another function : Replace .    If the key is stored for the first time , Just store the elements directly , return null. If the key doesn't exist for the first time , Just replace the previous value with the value , Returns the previous value )
  • clear();                                          Remove all key value pair elements
  • remove(Object key);                    Delete key value pair elements according to key , And return the value
  • containsKey(Object key);           Determines whether the set contains the specified key
  • containsValue(Object value);      Determines whether the set contains the specified value
  • isEmpty();                                    Determines if the set is empty
  • entrySet();                                    Returns the of a key value pair Set aggregate
  • get(Object key);                            Get value from key
  • keySet();                                        Get the set of all keys in the set , Put in Set aggregate
  • values();                                        Get the set of all values in the set , Put in Collection aggregate
  • size();                                            Returns the logarithm of key value pairs in a set, that is, the length of the set

6.Map Traversal of the set

  • Key search (get(key))
  • Get the entire key value pair object (Node)
  • forEach Method
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapTest {
    public static void main(String[] args) {
        //map Traversal of the set 
        HashMap<String, String> hm = new HashMap<>();
        hm.put(" I ", " Like to draw ");
        hm.put(" you ", " I like playing table tennis ");
        hm.put(" He ", " I like listening to music ");
        hm.put(" it ", " I like to eat dried fish ");

        // The way 1: Key search 
        Set<String> keySet = hm.keySet();
        for (String key : keySet) {
            String value = hm.get(key);
            System.out.println(key + "===" + value);
        }


        // The way 2:  Get all key value pairs , A collection of objects 
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        for (Map.Entry<String, String> node : entries) {
            String key = node.getKey();
            String value = node.getValue();
            System.out.println(key + "====" + value);
        }
        System.out.println("====================================");

        // The way 3: forEach()
        hm.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String key, String value) {
                System.out.println(key + "====" + value);
            }
        });

    }
}

  Two .HashMap aggregate ( Element uniqueness , disorder )

1.HashMap brief introduction

        HashMap The data structure is a hash table ( Array + Linked list + Red and black trees ), Element uniqueness , disorder .(HashMap Key uniqueness , It is rewritten by keys hashCode and equals() Method to guarantee , If you do not override , The uniqueness of the key cannot be guaranteed .)HashMap Collection allows inserting null key null value .

2.HashMap And Hashtable The difference between

  • HashMap Can be stored null value null key ; Hashtable He key or value , It's not allowed to be null value .
  • HashMap Thread unsafe , Efficient ; Hashtable Thread safety , Low efficiency .

3、 ... and .LinkedHashMap aggregate ( Element uniqueness , Orderly )

The underlying data structure is linked list and hash table , The elements are ordered and unique . The order of elements is guaranteed by the linked list data structure , Uniqueness by Hash table data structure guarantee . ( Keep in mind that : Map The data structure of a collection is only related to keys )

import java.util.LinkedHashMap;

public class LinkedHashMapTest {
    public static void main(String[] args) {
        
        LinkedHashMap<String, String> lh = new LinkedHashMap<>();

        lh.put(" I ", " Like to draw ");
        lh.put(" you ", " I like playing table tennis ");
        lh.put(" He ", " I like listening to music ");
        lh.put(" it ", " I like to eat dried fish ");

        System.out.println(lh);
    }
}

Four .TreeMap aggregate ( Element uniqueness , Ordered and sortable )

TreeMap The data structure of the key is a red black tree , It can ensure the sorting and uniqueness of keys . Sorting is divided into natural sorting and comparator sorting , The general default is natural sorting .  Threads are not safe .

About sorting and TreeSet The assembly is the same . It's just that the collections created are different !  ( To learn about sorting, click here )

package com.xingyun.test;

import java.util.Comparator;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class TreeMapTest {
        public static void main(String[] args) {

            //1.  Natural ordering 

            // Sort by age 
            TreeMap<Student,String> treeMap = new TreeMap<>();     // Space parameter structure 
            treeMap.put(new Student(" Zhang San ", 23)," love your life ");
            treeMap.put(new Student(" Li Si ", 27)," love JAVA");
            treeMap.put(new Student(" Lau Andy ", 21)," Love football ");
            treeMap.put(new Student(" Ouyang Zhenhua ", 28)," Love painting ");
            treeMap.put(new Student(" Zhang San ", 23)," Love music ");

            treeMap.forEach(new BiConsumer<Student, String>() {
                @Override
                public void accept(Student student, String s) {
                    System.out.println(student.getAge()+"==="+student.getName()+"==="+s);
                }
            });

            /*
            21=== Lau Andy === Love football 
            23=== Zhang San === Love music 
            27=== Li Si === love JAVA
            28=== Ouyang Zhenhua === Love painting 
            */

            // Comparator sort 
            TreeMap<Student,String> treeMap2 = new TreeMap<>(new Comparator<Student>() {
                // Anonymous inner class 
                @Override
                public int compare(Student s1, Student s2) {
                    // Sort by name length 
                    int num = s1.getName().length() - s2.getName().length();
                    // If the length of the name is the same, it depends on the content of the name 
                    int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                    // All the same , Sort by age 
                    int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
                    return num3;
                }
            });
            treeMap2.put(new Student(" Zhang San ", 23)," love your life ");
            treeMap2.put(new Student(" Li Si ", 27)," love JAVA");
            treeMap2.put(new Student(" Lau Andy ", 21)," Love football ");
            treeMap2.put(new Student(" Ouyang Zhenhua ", 28)," Love painting ");
            treeMap2.put(new Student(" Zhang San ", 23)," Love music ");

            treeMap2.forEach(new BiConsumer<Student, String>() {
                @Override
                public void accept(Student student, String s) {
                    System.out.println(student.getName()+"==="+student.getAge()+"==="+s);
                }
            });

            /*
             Zhang San ===23=== Love music 
             Li Si ===27=== love JAVA
             Lau Andy ===21=== Love football 
             Ouyang Zhenhua ===28=== Love painting 
            */
        }
    }

( Xiaobian is also trying to learn more ! I'll share it later !)

Hope to be helpful to friends !!!!

原网站

版权声明
本文为[naoguaziteng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210624100286.html