Map collection framework
Map collection framework
Introduction to Map collection framework
Method classification
A set of mapping relationships is stored in the map set, with key = value
Supplement: pseudocode described by Map set mapping relation May.Entry
Add & remove
Increase: put(K key, V value) putAll(Map<? extends K,? extends V> m) //Delete: clear() remove(Object key)
Judgment & Acquisition
Judgement: containsKey(Object key) containsValue(Object value) isEmpty() //Obtain: get(Object key) size() values() entrySet() keySet()
Compare the differences between Hashtable, HashMap and TreeMap
Be careful:
When adding an element, if the key already exists in the collection, the value added later will overwrite the original value, and the put method will return the original value
Map | data structure | Characteristic |
---|---|---|
Hashtable | The bottom layer is the hash table data structure | Cannot store null key and null value, thread synchronization, jdk1.0, low efficiency |
HashMap | The bottom layer is the hash table data structure | Null value and null key are allowed, and the thread is not synchronized. Replace Hashtable; jdk1.2, high efficiency |
TreeMap | The bottom layer is a binary tree data structure | The thread is not synchronized and can be used to sort the keys in the Map collection |
Map collection framework application
Application 1
package com.Map; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; /** * Application 1: 1,The student is used as the key, the address is used as the value for storage, and the name with the same age is recognized as a person, and finally output 2,Last sorted by age 3,Demand change, sort by name * *Steps: *1,Create a student class, instantiate it and add it to the map set as key *2,Judge students by weight * * @author 86182 * */ public class TreeMapDemo { public static void main(String[] args) { //Map<Student, String> map = new HashMap<>(); //Map<Student, String> map = new TreeMap<>(); Map<Student, String> map = new TreeMap<>(new StuComp()); map.put(new Student("aa", 25), "The Republic of Korea"); map.put(new Student("ee", 21), "Macao"); map.put(new Student("bb", 23), "Beijing"); map.put(new Student("dd", 26), "The Republic of Korea"); map.put(new Student("cc", 21), "Thailand"); map.put(new Student("dd", 25), "The Republic of Korea"); //Duplicate elements cannot be added because the underlying calls the hashCode and equals methods. If the elements are at the same address, the equals method will be called. //Not the same address System.out.println(map.size());//6 5 Set<Entry<Student, String>> entrySet = map.entrySet(); for (Entry<Student, String> entry : entrySet) { System.out.println(entry.getKey()+"=="+entry.getValue()); } } } class Student implements Comparable<Student>{ private String name; private int 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; } public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { super(); } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } @Override public int hashCode() { return this.getName().hashCode()+this.getAge()*39;//Widen the gap between hash values } @Override public boolean equals(Object obj) { Student stu = (Student)obj; return this.getAge() ==stu.getAge() && this.getName().equals(stu.getName()); } @Override public int compareTo(Student o) { int num = this.getAge() - o.getAge(); if(num == 0) { return this.getName().compareTo(o.getName()); } //Sort by age return num; } } class StuComp implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int num = o1.getName().compareTo(o2.getName()); if(num == 0) { return o1.getAge()-o2.getAge(); } //Sort by name return num; } }
Application two
package com.Map; import java.util.HashMap; import java.util.Map; /** * * map Generics in a collection use wrapper classes or application data types * * Application two: Count the number of characters in the string adsfdsfsfaadfsfdsfas Sort by times * *Function: statistical sorting * * Statistics: * 1,Convert string to character array * 2,Take the characters in this string as the key in the map set, and the number of occurrences as the value value * 3,When the character first appears, change the null returned to 1 * 4,If the character does not appear for the first time, the value corresponding to the character will be + 1 * * * @author 86182 * */ public class HashMapDemo { public static void main(String[] args) { String str = "basdfsfasaasadsfadsfa"; str = count(str); System.out.println(str); } /** * Character occurrence statistics * count-frequency * * @param str * @return */ private static String count(String str) { char[] charArray = str.toCharArray(); Map<Character, Integer> map = new HashMap<>(); for (char c : charArray) { Integer value = map.get(c); if(value == null) { map.put(c, 1); }else { map.put(c, value+1); } } StringBuffer sb = new StringBuffer(); for(Map.Entry<Character, Integer> entry:map.entrySet()) { sb.append(entry.getKey()+"("+entry.getValue()+")"); } return sb.toString(); } }