HaspMap use, report errors, out of order problems

Simple use of HaspMap

Simple addition, deletion and modification of HashMap

package map集合;

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

public class MapTest {
    
    
	 	public static void main(String[] args) {
    
    
			Map map = new HashMap();
			//成对放入多对 key-value
			map.put("java",109);
			map.put("ios",10);
			map.put("js",79);
			//value值可以重复
			map.put("python",99);
			//放入重复的key时,输出的是被覆盖的key的value值
			System.out.println(map.put("js", 29));
			//输入集合中的所有键值对
			System.out.println(map);
			
			//判断是否包含某个key
			System.out.println(map.containsKey("js"));//true
			//判断是否包含某个值
			System.out.println(map.containsValue(10));//true
			
			//获取Map集合的所有key组成的集合,通过遍历key来实现所有的键值对
			for(Object key:map.keySet()) {
    
    
				System.out.println(key+"---"+map.get(key));
			}
			//删除某个键值对
			map.remove("js");
			System.out.println(map);
		}
		//当两个hashMap的键值相同时,比较他们的值
		Map<Object,Object> mapOne= new HashMap<Object,Object>(); 
			Map<Object,Object> mapTwo= new HashMap<Object,Object>(); 
			mapOne.put(1, 1);
			mapOne.put(2, 2);
			mapTwo.put(3, 3);
			mapTwo.put(4, 4);
			
			for(Object key:mapOne.keySet()) {
    
    
				System.out.println(mapOne.get(key)==mapTwo.get(key));
			}

}

HashMap storage container

 HashMap<String,Vector<Object>> nowMessage =new HashMap<String,Vector<Object>>()

Error when deleting elements during HashMap traversal

Map<String,Object> searchMap = new HashMap<String,Object>();
            searchMap.put("name", name);
			searchMap.put("gender",gender);
			searchMap.put("idcard",idcard);
			searchMap.put("profession",profession);
for(Object key:searchMap.keySet()) {
    
    			if(searchMap.get(key).equals("")||searchMap.get(key).equals("请选择")) {
    
    
           //删除对应的键值对
				searchMap.remove(key,searchMap.get(key));
			}
			}

Result:
error:
java.util.ConcurrentModificationException HashMap
error reason:
HashMap is not thread-safe, ConcurrentHashMap is thread-safe.
Solution
Method 1: Simple and rude: Change HashMap to ConcurrentHashMap

Map<String,Object> searchMap = new ConcurrentHashMap<String,Object>();
			searchMap.put("name", name);
			searchMap.put("gender",gender);
			searchMap.put("idcard",idcard);
			searchMap.put("profession",profession);
			for(Object key:searchMap.keySet()) {
    
    
	         if(searchMap.get(key).equals("")||searchMap.get(key).equals("请选择")) {
    
    
				searchMap.remove(key,searchMap.get(key));
			}

Method 2: Perform a lock operation in the context of modifying the HashMap (not very understanding)

HashMap element sorting out of order problem

HashMap stores elements out of order, while LinkedHashMap is in order. If you need to follow the order, you can change HashMap to LinkedHashMap.

The problem of disordered ordering of HashMap elements and errors of deleting elements during HashMap traversal are solved at the same time.

Question:
Suppose I want to delete some key-value pairs in a certain collection, and the order of this collection cannot be changed. However, there are security issues when LinkedHashMap is deleted, and ConcurrentHashMap is out of order.
Solution:
Use the operation of not deleting but adding.
Create a new collection and traverse the previous collection. Add selection conditions during traversal, and add elements that meet the conditions to the new collection.

			String name = this.nameSearch.getText();
			String gender = this.genderSearch.getSelectedItem().toString();
			String idcard= this.idcardSearch.getSelectedItem().toString();
			String profession = this.professionSearch.getSelectedItem().toString();
            //所有搜索内容的集合   此处用两个linkedMap的原因    因为用for循环删除不需要的内容时会爆安全问题,需要用ConcurrentHashMap,但是他是不排序的,所以只能用两个linkedHashMap
			Map<String,Object> searchMap = new LinkedHashMap<String,Object>();
			searchMap.put("name", name);
			searchMap.put("gender",gender);
			searchMap.put("profession",profession);
			searchMap.put("idcard",idcard);
			//可用于挑选的内容集合
			Map<String,Object> searchMaps = new LinkedHashMap<String,Object>();
			
			for(Object key:searchMap.keySet()) {
    
    
			if(searchMap.get(key).equals("")==false||searchMap.get(key).equals("请选择")==false) {
    
    
				searchMaps.put((String) key,searchMap.get(key));
			}
			}

Guess you like

Origin blog.csdn.net/zhang19903848257/article/details/106022478