Collection framework -- Map

q sorting out the knowledge in this section

 

catalogue

This section combs the knowledge

preface

1, Characteristics

2, Traversal mode

1.entrySet

2.keyset

3, Common implementation class HashMap

4, Generics

5, Collection framework tool class

1.Collections

2.Arrays

summary

preface

Previously, I introduced the list set. Next, I will also introduce the Map set in the collection framework. Let's have a look!

 

1, Characteristics

① addition, deletion, modification and query

② key value pairs exist

③ key can be empty

Example code:

package com.xhy.map;

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

/**
 * map Characteristics of sets
 * @author zjjt
 *
 */
public class demo1 {
public static void main(String[] args) {
	//① Key value pair form
	Map<String, Object> map=new HashMap<>();
	//② Increase
	map.put("a",1);
	map.put("b",2);
	map.put("c",3);
	map.put("d",4);
	System.out.println(map);
	System.out.println("========================");
	//③ Delete
	System.out.println(map.remove("b"));
	System.out.println("=========================");
	//④ Change (direct coverage)
	map.put("d", 5);
	System.out.println(map);
	System.out.println("==========================");
	//⑤ Check
	System.out.println(map.get("a"));
	System.out.println("==========================");
	//key can be empty
	map.put(null, 111111);
	System.out.println(map.get(null));
}
}

 

2, Traversal mode

Diagram of storage method of Map collection:

 

1.entrySet

The Set collection of mapping relationships contained in this mapping. Pack the key and value mapping relationship in the Map Set into an object and store it in the Set set

The code is as follows (example):

①.Get first map Set mapping relationship
 Example code:
Set<Entry<String,Object>> entrySet = map.entrySet();
		for (Entry<String, Object> e : entrySet) {
			System.out.println(e);
		}

Output results:

 

2.keyset

Store all the keys in the Map Set into the Set set, and then obtain the value corresponding to each key according to the get() method (because the Set set has an iterator, you can also get all the keys through the iterative method, and then obtain the value corresponding to each key according to the get() method)

The code is as follows (example):

Map<String, Object> map=new HashMap<>();
	map.put("a",1);
	map.put("b",2);
	map.put("c",3);
	//1. Use the keySet() method to convert to a Set set
		System.out.println("--------------keySet()method foreach ergodic ---------------");
	//Get all the key s in the map set first
	Set<String> keys=map.keySet();
	for(String key : keys) {
		System.out.println("key:"+key+";value="+map.get(key));
	}
	
	System.out.println("--------------keySet()Method iterator traversal ---------------");
	Set<String> keySet1 = map.keySet();
	Iterator<String> iterator = keySet1.iterator();
		while(iterator.hasNext()) {
			String key=iterator.next();
			Object value=map.get(key);
			System.out.println("key:"+key+";value="+value);
		}

Output results:

 

3, Common implementation class HashMap

There are hashMap and TreeMap under the Map set (less used)

HashMap: the key for synchronization is not null, and the value cannot be null

Example: count the number of occurrences of each letter in a string

Example code:

String s="sankjhaijnbsdbandvafdyiqwdabcxnmdssmanbscmba";
	char[] chars = s.toCharArray();
	Map<Character, Integer>map=new HashMap<Character, Integer>();
	for (char c : chars) {
		Integer num=map.get(c);
		if(num==null || num==0) {
			map.put(c, 1);
		}else {
			map.put(c, num+1);
		}
	}
	Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
	System.out.println(entry.getKey()+"There it is"+entry.getValue()+"second");
}
}

Output results:

 

 

4, Generics

Concept:

Usually, when writing a class, interface, or method, the data type is incorrect or uncertain. You can use < letter > to represent an uncertain data type

Functions: ① convert the exceptions generated during runtime into compile time errors

② improve the robustness of the code

* you can understand generics at present

① Generic class

class commomDao<T>{
	public List<T> getAll(T a){
		System.out.println("General query method......");
			return null;
	}
}
/**
 * Book Entity class
 * @author T440s
 *
 */
class Book{
	private String name;	
}
/**
 * Order Entity class
 * @author T440s
 *
 */
class Order{
	private String name;
}
/**
 * Bookdao Entity class
 * @author T440s
 *
 */
class BookDao extends commomDao<Book>{
	@Override
	public List<Book> getAll(Book a) {
		// TODO Auto-generated method stub
		return super.getAll(a);
	}
}
/**
 * Orderdao Entity class
 * @author T440s
 *
 */
class OrderDao extends commomDao<Order>{
	@Override
	public List<Order> getAll(Order a) {
		// TODO Auto-generated method stub
		return super.getAll(a);
	}

}

② Generic method:

public <T>  List<T> getAll(T a){
		System.out.println("General query method......");
			return null;
}

 

5, Collection framework tool class

1.Collections

//Collections sort
package com.xhy.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * list Collection de duplication is related to the de duplication object equals method
 * @author zjjt
 *
 */
public class Demo5 {
public static <Syudent> void main(String[] args) {
	List<Student> list = new ArrayList<>();
	list.add(new Student("xhy", 1000));
	list.add(new Student("sjy", 3000));
	list.add(new Student("ltf", 4000));
	list.add(new Student("lsz", 10000));
	//The Collections class implements sorting
		Collections.sort(list, new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				return o1.getMoney()-o2.getMoney();
			}

		});
		for (Student student : list) {
			System.out.println(student);
		}

}
}

class Student{
	private String name;
	private int money;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", money=" + money + "]";
	}
	public Student(String name, int money) {
		super();
		this.name = name;
		this.money = money;
	}
	public Student() {
		// TODO Auto-generated constructor stub
	}
	
}

Output results:

 

2. Common methods of arrays

1.toString(): print out all the contents of the array

2.sort(): array sorting

3.asList(): convert an array into a set (not against the characteristics of the array itself)

4.equals(): compare whether the array elements are equal

Example code:

package com.xhy.map;

import java.util.Arrays;
import java.util.List;

public class demo6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] arr=new int [] {37,65,767,23,243,62,77,90};
		int [] arr2=new int [] {767,23,243,62,77,90};
		//Print
		System.out.println(Arrays.toString(arr));
		//Turn an array into a collection
		List<int[]> as = Arrays.asList(arr);
		//Compare array elements for equality
		boolean fool = Arrays.equals(arr, arr2);
		//sort
		Arrays.sort(arr);

	}

}

Output results:

 

Keywords: Java

Added by dschreck on Fri, 14 Jan 2022 00:24:33 +0200