Java collection system (b) - Map System

1、Map

   Map for storing data having a mapping relationship, and therefore the collection Map holds two sets of values, a set of values stored in the Map key, a set of stored in the Map value, key value, and may be any type of data reference . Map of the key can not be duplicated, that comparison is always a key with any two Map objects by the equals method returns false.
  there is a one to one relationship between the key and the way value, specified by the key, to find the corresponding value. There is a keySet () Map method, it is possible to obtain all of the key, since the key can not be repeated and disordered, so a key set is essentially a collection of collections, a value list looks like is set.
Here Insert Picture Description
Map common methods Interface
void clear (): Delete the Map object all kv of
boolean containsKey (Object key): Discover the Map object contains the specified Key
boolean containsValue (Object value): whether to query the Map object that contains the specified value
the Set entrySet (): returns the kv Map object contains a set consisting of set, set elements are set Map.Entry (Entry Map is the internal) Object
Object get (Object key): returns the key corresponding to the specified value; if the Map object does not contain the key, null is returned
boolean isEmpty (): query the Map object is empty
Set keySet (): returns the Set collection of the Map object composed of all key
Object put (Object key, Object value ): to Map object to add a couple of kv
Object remove (Object key): kv to delete the specified key corresponding to
delete the specified key and a value corresponding to kv to successfully deleted True Returns: boolean the Remove (Object key, Object value)
kv return to the Map object: int size () The number of

2、HashMap和Hashtable

  HashMap and Hashtable are Map interface implementation class, similar to the relationship ArrayList and the Vector. In the actual development, HashMap usage is much larger than Hashtable. From the naming point of view, you know Hashtable class is an old (now failed to comply with the naming conventions).
  I want to store data HashMap and Hashtable, as key objects must implement the hashCode () method and equals () methods.

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author RuiMing Lin
 * @date 2020-03-05 16:01
 */
public class Person {
    //使用Person类所为key
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return getAge() == person.getAge() &&
                Objects.equals(getName(), person.getName());
    }
    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    public static void main(String[] args) {
        HashMap<Person, String> hmap = new HashMap<>();
        hmap.put(new Person("小明", 18), "是个坏学生");
        hmap.put(new Person("小红", 17), "是个好学生");
        hmap.put(new Person("小刚", 19), "是个学霸");
        Set<Map.Entry<Person, String>> entrySet = hmap.entrySet();
        for (Map.Entry<Person, String> entry : entrySet) {
            Person person = entry.getKey();
            String string = entry.getValue();
            System.out.println("person = " + person + "..." + "string = " + string);
        }
    }
}

3, Laidakedःashanap

LinkedHashMap is a subclass of HashMap, usage almost the same, but different implementations of the storage order. Underlying LinkedHashMap is to use a hash table with a doubly linked list to hold all the elements, so the iteration order LinkedHashMap and insert in the same order.

import java.security.Key;
        import java.util.LinkedHashMap;
        import java.util.Map;
        import java.util.Set;

/**
 * @author RuiMing Lin
 * @date 2020-03-05 16:14
 */
public class LinkedHashMapDemo {
    public static void main(String[] args) {
        LinkedHashMap<String,Integer> lsmap = new LinkedHashMap();
        lsmap.put("小明", 18);
        lsmap.put("小红", 19);
        lsmap.put("小刚", 20);
        Set<Map.Entry<String, Integer>> entrySet = lsmap.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("key = " + key + "..." + "value = " + value);
        }
    }
}

4、SortedMap和TreeMap

SortedMap and TreeMap is a blog on the principle of the SortedSet and TreeSet like to view details on a blog.

Please indicate the wrong place! Thought that it was in trouble if you can give a praise! We welcome comments section or private letter exchange!

Published 30 original articles · won praise 72 · views 10000 +

Guess you like

Origin blog.csdn.net/Orange_minger/article/details/104571897