Advanced Java - collection framework 3

Keywords: PHP Java

1. Map

1.1 basic concepts

Common methods in 1.2Map

package cn.jxufe.java.chapter7;

import java.util.HashMap;
import java.util.Map;

public class Test09Map {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        function();
        System.out.println();
        function_1();
        System.out.println();
        function_2();
    }

    /*
     *  Remove the key value pair in the collection and return the value before being removed
     *  V remove(K)
     */
    public static void function_2() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        System.out.println(map);

        String value = map.remove(3);
        System.out.println(value);
        System.out.println(map);
        System.out.println(map.size());
        System.out.println(map.values());
        System.out.println(map.keySet());
        System.out.println(map.containsKey(2));
        System.out.println(map.containsValue("b"));
        System.out.println(map.entrySet());
    }

    /*
     * Get value object through key object
     * V get(K)
     * If there is no such key in the collection, return null
     */
    public static void function_1() {
        // Create collection object,Object integer as key,Object store string for value
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        System.out.println(map);

        String value = map.get(4);
        System.out.println(value);

        String value2 = map.get(3);
        System.out.println(value2);
    }

    /*
     *  Store key value pairs in the collection
     *  V put(K,V) K Object as key, object with V as value
     *  What is stored is a duplicate key, which overwrites the original value
     *  Return value normally returns null,
     *  When the duplicate key is stored, the value before being overwritten is returned
     */
    public static void function() {
        // Create collection object,HashMap,Storage object,Key is a string,The value is integer.
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);

        map.put("b", 2);

        map.put("c", 3);

        System.out.println(map);
    }

}

1.3 traversal of map

Method 1:

package cn.jxufe.java.chapter7;

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

/*
 *  Map Traversal of set
 *    Get value with key
 *    Map Define method keySet in interface
 *    All keys, stored in Set set Set
 */
public class Test10Map {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 11);
        map.put("b", 12);
        map.put("c", 13);
        map.put("d", 14);
        map.put("ab", 14);

        // 1. call map Methods of collection keySet,All keys stored in Set Collection
        Set<String> set = map.keySet();
        System.out.println(set);
        // 2. ergodic Set aggregate,Removed Set All elements in the collection (Map Key in)
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            // it.next Return is Set Set element,that is Map Key in
            // 3. call map Ensemble method get,Get value by key
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key + "...." + value);
        }
        System.out.println("=======================");
        for (String key : map.keySet()) {
            Integer value = map.get(key);
            System.out.println(key + "...." + value);
        }

    }

}

Method 2:

In the Map class design, a nested interface is provided: entry. Entry encapsulates the corresponding relationship of key value pairs into objects. That is, key value pair object, so when we traverse the Map collection, we can get the corresponding key and corresponding value from each key value pair object.

  • entrySet() method: used to return all key value pair (Entry) objects in the Map collection, in the form of Set collection.
package cn.jxufe.java.chapter7;

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

/*
 *  Map Collection acquisition method
 *  entrySet Method, key value pair mapping (marriage certificate) acquisition
 *  Implementation steps:
 *    1. Call the map collection method entrySet() to store the mapping relationship objects in the collection into the Set collection
 *        Set<Entry <K,V> >
 *    2. Iterative Set set
 *    3. The element of the retrieved Set set is a mapping relation object
 *    4. Get the key value pair through the mapping relation object method getKey, getValue
 *    
 *    Create inner class object outer class. Inner class = new 
 */
public class Test11Map {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "abc");
        map.put(2, "bcd");
        map.put(3, "cde");
        // 1. call map Ensemble method entrySet()Mapping relationship objects in a collection,Store to Set aggregate
        Set<Map.Entry<Integer, String>> set = map.entrySet();
        // 2. iteration Set aggregate
        Iterator<Map.Entry<Integer, String>> it = set.iterator();
        while (it.hasNext()) {
            // 3. Removed Set Elements of a collection,Is a mapping relationship object
            // it.next What object to get,Also Map.Entry object
            Map.Entry<Integer, String> entry = it.next();
            // 4. By mapping relational object methods getKet, getValue Get key value pair
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "...." + value);
        }

        System.out.println("=========================");
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "..." + entry.getValue());
        }
    }

}

1.4 HashMap stores custom type key values

package cn.jxufe.java.chapter7;

import java.util.HashMap;
import java.util.Map;

/*
 *  Use the HashMap collection to store custom objects
 *  Custom objects, as keys, appear, appear as values
 */
public class Test12HashMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*
         * HashMap Store custom object Person as key
         * The object of the key is of type Person and the value is a string
         * Ensure the uniqueness of the key, store the object to the key, and rewrite hashCode equals
         */
        HashMap<Person, String> map = new HashMap<Person, String>();
        map.put(new Person("a", 20), "Rio de Janeiro");
        map.put(new Person("b", 18), "Somalia");
        map.put(new Person("b", 18), "Somalia");
        map.put(new Person("c", 19), "Bermuda");
        for (Person key : map.keySet()) {
            String value = map.get(key);
            System.out.println(key + "..." + value);
        }
        System.out.println("===================");
        for (Map.Entry<Person, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "..." + entry.getValue());
        }
    }
}

1.5LinkedHashMap

1.6TreeMap

package cn.jxufe.java.chapter7;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Test13TreeMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("smith", 30);
        hashMap.put("pull", 31);
        hashMap.put("kobe", 29);
        hashMap.put("weide", 29);
        System.out.println("Display entries in hashMap");
        System.out.println(hashMap + "\n");
        
        Map<String,Integer> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("smith", 30);
        linkedHashMap.put("pull", 31);
        linkedHashMap.put("kobe", 29);
        linkedHashMap.put("weide", 29);
        System.out.println("Display entries in linkedHashMap");
        System.out.println(linkedHashMap + "\n");
        
        Map<String,Integer> treeMap = new TreeMap<>();
        treeMap.put("smith", 30);
        treeMap.put("pull", 31);
        treeMap.put("kobe", 29);
        treeMap.put("weide", 29);
        System.out.println("Display entries in treeMap");
        System.out.println(treeMap);  
    }
}

2. Example learning: number of words

Posted by CountryGirl on Sat, 02 Nov 2019 01:48:28 -0700