Java collection framework (Map)

Map collection

  • Features of Map interface
    • Used to store any key value pair
    • Key: no repetition, no subscript
    • Value: unordered, no subscript, duplicate allowed
  • Common methods of Map interface
    • V get(Object key): returns the value corresponding to the specified key object in the Map set. V indicates the data type of the value
    • V put(K key, V value): add a key value pair to the Map set, and return the value corresponding to the previous key. If not, return null
    • V remove(Object key): delete the key value pair corresponding to the key from the Map set, and return the value corresponding to the key. If not, return null
    • Set entrySet(K,V): returns the set set of all key value pairs in the Map set. The data type of the elements in this set is Map Entry
    • Set keySet(K): returns the set set of all key objects in the Map set
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("cn","china");
        map.put("cn","China");
        map.put("en","english");
        map.put("us","american");
        System.out.println(map.size());
        System.out.println(map);
        //ergodic
        //Set<String> set = map.keySet();
        for (String s : map.keySet()) {
            System.out.println(s+":"+map.get(s));
        }
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry);
            //System.out.println(entry.getKey()+":"+entry.getValue());
        }

Implementation class of Map collection

  • HashMap:
    • Unsafe thread and fast running efficiency; null is allowed as key or value
    • Storage structure: hash table
    • HashMap() :
      Construct an empty HashMap with default initial capacity (16) and default load factor (0.75)
    public static void main(String[] args) {
        HashMap<Student, String> sh = new HashMap<>();
        Student s1 = new Student("sun", 111);
        Student s2 = new Student("qian", 222);
        Student s3 = new Student("wang", 333);
        sh.put(s1,"beijing");
        sh.put(s2,"shanghai");
        sh.put(s3,"beijing");
        System.out.println(sh);
        for (Student student : sh.keySet()) {
            System.out.println(student+":"+sh.get(student));
        }
        for (Map.Entry<Student, String> stu : sh.entrySet()) {
            //System.out.println(stu);
            System.out.println(stu.getKey()+"Address:"+stu.getValue());
        }
        System.out.println(sh.containsKey(s1));
        System.out.println(sh.containsValue("nanjing"));
        System.out.println(sh.isEmpty());
    }
  • summary
    • When HashMap was first created, the table was null. To save space, when the first element was added, the table was adjusted to 16
    • When the number of elements is greater than the threshold (16 * 0.75), the capacity will be expanded. After the expansion, the size will be twice that of the original, in order to reduce the number of adjusted elements
    • jdk1.8 when the length of each linked list is greater than 8 and the number of array elements is greater than or equal to 64, it will be adjusted to a red black tree in order to improve the execution efficiency
    • jdk1.8 when the length of the linked list is less than 6, it will be adjusted into a linked list
    • jdk1. Before 8, the chain header was inserted, jdk1 After 8 is tail insertion
  • HashTable (infrequently used):
    • JDK1. Version 0, thread safe and slow running efficiency; null is not allowed as key or value
  • Properties: inherits HashTable
    • Subclass of Hashtable. Both key and value are required to be strings. It is usually used for reading configuration files
  • TreeMap
    • Storage structure: red black tree
    • SortedMap interface (a sub interface of Map) is implemented, which can automatically sort key s
    public static void main(String[] args) {
        TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1 = o1.getName().compareTo(o2.getName());
                int n2 = o1.getId()-o2.getId();
                return n1==0?n2:n1;
            }
        });
        Student s1 = new Student("sun", 111);
        Student s2 = new Student("qian", 222);
        Student s3 = new Student("wang", 333);
        tm.put(s1,"Beijing");
        tm.put(s2,"Shanghai");
        tm.put(s3,"Nanjing");
        tm.put(new Student("wang", 333),"Shenzhen");
        System.out.println(tm.size());
        System.out.println(tm);
//        tm.remove(s3);
//        System.out.println(tm.size());
//        System.out.println(tm);
        //ergodic
        for (Student key : tm.keySet()) {
            System.out.println(key+":"+tm.get(key));
        }
        for (Map.Entry<Student, String> en : tm.entrySet()) {
            System.out.println(en);
        }

Collections utility class

  • Concept: collection tool class, which defines common collection methods other than access
  • method:
    • Reverse (list <? > list): reverse the order of elements in the set
    • Shuffle (list <? > list): randomly reset the order of set elements
    • Sort (list): sort in ascending order (element types must implement the Comparable interface)
    public static void main(String[] args) {
        ArrayList<Integer> in = new ArrayList<>();
        in.add(20);
        in.add(2);
        in.add(56);
        in.add(78);
        in.add(17);
        Collections.sort(in);
        System.out.println(in);
        //Binary search Binary Search
        System.out.println(Collections.binarySearch(in, 56));
        //Copy copy, ensure that the two arrays are the same size
        ArrayList<Integer> in2 = new ArrayList<>();
        for (int i = 0; i < in.size(); i++) {
            in2.add(0);
        }
        Collections.copy(in2,in);
        System.out.println(in2);
        //Reverse reverse
        Collections.reverse(in2);
        System.out.println(in2);
        //shuffle random disruption
        Collections.shuffle(in2);
        System.out.println(in2);
    }
  • Supplement: list to array, array to set
		//list to array
        Integer[] arr = in.toArray(new Integer[0]);
        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));
        //Array to set
        //This collection is limited and cannot be added or deleted
        List<Integer> list = Arrays.asList(arr);
        System.out.println(list);
        //The basic data type is converted to a collection and needs to be modified to a packing type
        Integer[] nums = {100,23,45,67};
        List<Integer> list1 = Arrays.asList(nums);
        System.out.println(list1);

Keywords: Java Back-end

Added by mattotto77 on Wed, 16 Feb 2022 14:15:28 +0200