This is the first day of my participation in Gwen Challenge

Map implementation class structure

Map: stores data in key-value pairs in two columns

  • HashMap: as the main implementation class of Map; Threads are unsafe and efficient. Store null keys and values
  • LinkedHashMap: Ensures that map elements are traversed in the order they were added.
    • Reason: Add a pair of Pointers to the previous and the next element to the underlying structure of the HashMap. This class performs more efficiently than a HashMap for frequent traversal operations.
  • TreeMap: Ensures sorting by the added key-value to implement sorting traversal. The natural or custom sort of key is used at this point. The bottom layer uses red black trees
  • Hashtable: as an ancient implementation class; Thread safety and low efficiency; Cannot store null keys and values
    • Properties: Used to process configuration files. Key-value is a String

Understanding the Map structure

Keys in Map: Unordered and non-repeatable. Use Set to store all keys

-> override equals () and hashCode ()Copy the code

Values in Map: Unordered and repeatable. Use Collection to store all values

->value overrides equals ()Copy the code

A key-value pair: key-value constitutes an Entry object.

Entries in a Map: Unordered and non-repeatable. Use Set to store all entries

What is the underlying implementation of HashMap?

In line with HashSet

Methods defined in the Map

Common methods:

  • Add: put
  • Delete: remove
  • Update: the put
  • Query: the get
  • Length: the size
  • Traverse: keyset/values/entrySet

All the methods

Add, delete, change

Put, putAll, remove, clear

        map.put("AA".123);// Reflect add
        map.put(45.123);
        map.put("BB".56);
        map.put("AA".87);// Reflect the changes

        System.out.println(map);
        System.out.println("* * * * * * * * * *");

        Map map1 = new HashMap<>();
        map1.put("CC".123);
        map1.put("DD".123);

        map.putAll(map1);
        System.out.println(map);
        System.out.println("* * * * * * * * * *");

        //remove
        Object value = map.remove("CC");
        System.out.println(value);
        System.out.println(map);
        System.out.println("* * * * * * * * * *");

        //clear
// map.clear();
// System.out.println(map.size());
// System.out.println(map);

Copy the code

The query

Get, containsKey, containsValue, size, isEmpty, equals

/ / query
        //get
        System.out.println(map.get(45));
        //containsKey
        System.out.println(map.containsKey(45));
        //containsValue
        System.out.println(map.containsValue(123));
        //size
        System.out.println(map.size());
        //isEmpty()
        System.out.println(map.isEmpty());
        //equals
        System.out.println(map.equals(map1));
        System.out.println("* * * * * * * * * *");
Copy the code

Traversal operation

 // iterate over the operation
        // Iterate over all key sets
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("* * * * * * * * * *");

        // Iterate over all values
        Collection values = map.values();
        for (Object obj : values) {
            System.out.println(obj);
        }
        System.out.println("* * * * * * * * * *");

        // Iterate over all key-values
        // Method 1:
        Set entrySet = map.entrySet();
        Iterator iterator1 = entrySet.iterator();
        while (iterator1.hasNext()) {
            Object obj = iterator1.next();

            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry.getKey()+"-- -- -- -- >"+entry.getValue());
        }
        System.out.println("* * * * * * * * * *");

        // Method 2:
        Set keySet = map.keySet();
        iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value1 = map.get(key);
            System.out.println(key+"-- -- -- -- >"+value1);
        }

Copy the code

TreeMap

Add a key-value pair to the TreeSet, requiring that the key be an object created in the same class

There are two kinds of sorting to be implemented: natural sorting and custom sorting

Natural ordering

class Student implements Comparable{... @Override public intcompareTo(Object o) {
        if (o instanceof Student) {
            Student s = (Student) o;
            int com = -this.name.compareTo(s.name);
            if(com ! =0) {
                return com;
            } else {
                return Integer.compare(this.age, s.age); }}else
            throw newRuntimeException(); }}Copy the code

Custom sorting

TreeMap map1 = new TreeMap(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Student && o2 instanceof Student) {
                    Student s1 = (Student) o1;
                    Student s2 = (Student) o2;
                    return Integer.compare(s1.getAge(), s2.getAge());
                } else {
                    throw new RuntimeException("Type mismatch"); }}});Copy the code

Properties

package MapInterface;import java.io.FileInputStream;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args)throws Exception {
        Properties pros = new Properties();

        FileInputStream fis = new FileInputStream("jdbc.properties");

        pros.load(fis);
        String name = pros.getProperty("name");
        String password = pros.getProperty("password");

        System.out.println("name= " + name + ",password= "+ password); }}Copy the code