Preface: tell you about the Map combination framework
Codeword is not easy. Pay attention
Reprint, please explain!
Mind map:
catalogue
2. Traversal method of map set
3. Common implementation classes HashMap and examples
3. Example: count the number of occurrences of each character in the string
5. Collection framework tool class
1. Characteristics of map set
1. You can add, delete, modify and query
2. Exists in the form of key value pairs
3. The key can be empty (null)
Code display:
/** * Map Characteristics of sets * key Key value * @author zjjt * */ public class Dome1 { public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("a", 1);// Method of adding map.put("b", 2); map.put("c", 3); map.put(null, 4);//The key value can be empty System.out.println(map.remove("a"));//The deleted method returns the value corresponding to the key map.put("a", 6);//Modify method, modify element value System.out.println(map.get("a")); //The query method returns the value corresponding to the key System.out.println(map); } }
The output results are as follows:
2. Traversal method of map set
Code display:
/** * Map Traversal mode of * * @author zjjt * */ public class Dome2 { public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); //Save all keys in the set set, and then traverse the output key value pairs Set<String> keySet = map.keySet(); for (String key : keySet) { System.out.println("key:"+key+"---"+"value:"+map.get(key)); } System.out.println("-----------------------"); //Get the mapping relationship of the Map set first Set<Entry<String, Object>> entrySet = map.entrySet(); for (Entry<String, Object> entry : entrySet) { System.out.println("key:"+entry.getKey()+"---"+"value"+entry.getValue()); } } }
The results are as follows:
3. Common implementation classes HashMap and examples
1. What is HashMap?
When it comes to containers, you will certainly think of object storage containers in Java, such as ArrayList, LinkedList, HashSet, etc. compared with these containers, HashMap can be understood as an additional layer of pointing relationship. You can use the specified Key to find the specified Value
2. Data structure:
1. The whole is an array
2. Each position of the array is a linked list
3. The Value in each node of the linked list is the Object we store
3. Example: count the number of occurrences of each character in the string
When you encounter problems, you must first learn to think
Step 1: convert string to character array
Step 2: traverse the array and put the characters into the key in the Map set
Step 3: judge that if there is no character, set the value corresponding to the character to 1; if there is, give the value corresponding to the character + 1
Code display:
/* * Counts the number of occurrences of characters in the string */ public class Dome3 { public static void main(String[] args) { String s = "asdfsdfdsnjfdvkjhdsnfsfjkjshscjsdosdfsdfxcsv"; char[] charr = s.toCharArray(); Map<Character, Integer> map = new HashMap<Character, Integer>(); for (char c : charr) { Integer num=map.get(c); if(num==null || num==0) { map.put(c, 1); }else { map.put(c, ++num); } } Set<Entry<Character, Integer>> entrySet = map.entrySet(); for (Entry<Character, Integer> entry : entrySet) { System.out.println(entry.getKey()+"There it is"+entry.getValue()+"second"); } } }
The output results are as follows:
4. Generics
1. Function
1. Type safety
2. Eliminate cast
3. Convert the exception generated at runtime into a compiler error
4. Better code reusability
Filter out the key s corresponding to even value s
The code is as follows:
map.put("o", "Zhang San")// A piece of data with key o and value Zhang San is inserted
When compiling, it tells you that there is no problem, and an error will be reported only when it is executed
At this time, the generic type specified by value is Integer, and the generic type has been specified. If you need to add three, you will get a compilation error
2. Generic class
//Define a generic class, class xxdao<H>{ public List<H> list(H h){ System.out.println("method"); return null; } } class Teacher{ } class Student{ } //Specify generic Teacher class TeacherDao extends xxdao<Teacher>{ @Override public List<Teacher> list(Teacher h) { // TODO Auto-generated method stub return super.list(h); } } //Specify generic Student class StudentDao extends xxdao<Student>{ @Override public List<Student> list(Student h) { // TODO Auto-generated method stub return super.list(h); } }
3. Generic methods
//Class and abstract class //Abstract methods are not allowed in a class //Abstract classes allow abstract methods //If it is not a generic class, the method cannot be internal. The generic method is not completely aligned //Generic methods solve the above problems class xxdao1<H>{ public <T> List<H> list(H h){ System.out.println("method"); return null; } }
5. Collection framework tool class
1.Collections
1. Sorting
2. Set to array: toArray
2.Arrays
1.toString
2.sort
3.asList
The code is as follows
/** * Collection framework tool class * * @author zjjt * */ public class Dome5 { public static void main(String[] args) { List<User> list = new ArrayList<>(); list.add(new User("Zhang San", 16)); list.add(new User("Li Si", 17)); list.add(new User("Wang Wu", 20)); list.add(new User("Zhao Liu", 18)); // Age descending sort Collections.sort(list, new Comparator<User>() { @Override public int compare(User o1, User o2) { return -(o1.getAge() - o2.getAge()); } }); for (User s : list) { System.out.println(s); } String[] arr = new String[] { "a", "h", "j", "p", "g", "w" }; //System.out.println(arr); You will get the address [Ljava.lang.String;@7852e922 /** * Get the value in the array */ // for (String string : arr) { // System.out.println(string); // } // toString System.out.println(Arrays.toString(arr)); //The operation of turning an array into a collection cannot violate the characteristics of the array itself //For example, the array length is immutable List<String> asList = Arrays.asList(arr); //asList.add("4"); An error will be reported during operation /** * Bubble sorting */ Integer[] in=new Integer[] {5,3,8,7}; Arrays.sort(in); System.out.println(Arrays.toString(in)); } } class User { 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } public User(String name, int age) { super(); this.name = name; this.age = age; } }
IT's over here. I'm still the primary school student studying IT
You are welcome to give advice