00020.07 Collection Map interface and its implementation classes (including HashMap, Hashtable, TreeMap, LinkedHashMapProperties and implementation of Comparable interface templates)

Series Article Directory

Preface

This section talks about its implementation class

1. Implementation classes of the Map interface:

1、HashMap
2、Hashtable
3、TreeMap
4、LinkedHashMap
5、Properties

Two, HashMap and Hashtable hash table

Hashtable: Old version. Thread safe. Its key and value cannot be null.
HashMap: It is a bit newer than Hashtable. Thread is not safe. It allows the key and value to be null values.
And their sorting is also slightly different.
Insert picture description here
Associated memory: (older writes before)
StringBuffer and StringBuilder
Vector and ArrayList
Hashtable and HashMap

Three, HashMap and LinkedHashMap

LinkedHashMap is a subclass of HashMap, which maintains the order of adding mapping relations more than HashMap.
HashMap: Unordered
LinkedHashMap: You can record the order of addition.
Insert picture description here
Insert picture description here

LinkedHashMap has more things to do than HashMap, and it is inefficient. So it is recommended that we only use it when we need to maintain the order.

Four, HashMap and TreeMap

HashMap: disordered.
TreeMap: in order of key size.

Five, Properties

Properties is a subclass of Hashtable, which does not allow the key and value to be null, and its key and value types are both String.
Usually used to store configuration properties.
And for better readability, two methods have been added:
setProperty(key,value)
String getProperty(key)
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Note that the red box above is wrong
Note that the red box above is wrong, it is System.getProperties()
Insert picture description here
Insert picture description here

6. The keys of all maps cannot be repeated. How to achieve non-repetition?

HashMap, Hashtable, LinkedHashMap, Properties: According to the hashCode and equals methods
of the key. TreeMap: According to the size of the key, two keys of equal size are considered to be duplicates.
If the key is duplicated, the following value will replace the original value.
To make the key row size for TreeMap, either the key type itself implements the java.lang.Comparable interface, or when creating the TreeMap, specify an implementation class object of the java.util.Comparator interface.
Insert picture description here
Insert picture description here

Seven, complete code

package com.atguigu.test06;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

import org.junit.Test;

@SuppressWarnings("all")
public class TestMapImpl {
    
    
	
	//key不可重复
	@Test
	public void test7(){
    
    
		Map map = new HashMap();
		
		map.put("杨洪强", "翠花");//被覆盖了
		map.put("崔志恒", "如花");
		map.put("甄玉禄", "凤姐");
		map.put("杨洪强", "冰冰");
		
		Set entrySet = map.entrySet();
		for (Object entry : entrySet) {
    
    
			System.out.println(entry);
		}
	}
	
	
	@Test
	public void test6() throws IOException{
    
    
		Properties pro = System.getProperties();//获取系统属性配置
		Set entrySet = pro.entrySet();
		for (Object entry : entrySet) {
    
    
			System.out.println(entry);
		}
	}
	
	@Test
	public void test5() throws IOException{
    
    
		Properties pro = new Properties();
		pro.load(TestMapImpl.class.getClassLoader().getResourceAsStream("jdbc.properties"));
		
		String user = pro.getProperty("user");
		String password = pro.getProperty("password");
		System.out.println(user);
		System.out.println(password);
	}
	
	@Test
	public void test4(){
    
    
		Properties pro = new Properties();
		pro.setProperty("user", "chailinyan");
		pro.setProperty("pwd", "123456");
		
		String user = pro.getProperty("user");
		String password = pro.getProperty("pwd");
		System.out.println(user);
		System.out.println(password);
	}
	
	@Test
	public void test3(){
    
    
		Map map = new TreeMap();//按照key排大小顺序。
		
		map.put("yanghongqiang", "翠花");
		map.put("cuizhiheng", "如花");
		map.put("zhenyulu", "凤姐");
		map.put("suhaibo", "翠花");
		
		Set entrySet = map.entrySet();
		for (Object entry : entrySet) {
    
    
			System.out.println(entry);
		}
	}
	
	@Test
	public void test2(){
    
    
		Map map = new LinkedHashMap();//按照添加顺序
		
		map.put("杨洪强", "翠花");
		map.put("崔志恒", "如花");
		map.put("甄玉禄", "凤姐");
		map.put("苏海波", "翠花");
		
		Set entrySet = map.entrySet();
		for (Object entry : entrySet) {
    
    
			System.out.println(entry);
		}
	}
	
	@Test
	public void test1(){
    
    
		Map map = new HashMap();//完全无序
		
		map.put("杨洪强", "翠花");
		map.put("崔志恒", "如花");
		map.put("甄玉禄", "凤姐");
		map.put("苏海波", "翠花");
		
		Set entrySet = map.entrySet();
		for (Object entry : entrySet) {
    
    
			System.out.println(entry);
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_41753340/article/details/109362259