当前位置:网站首页>Map value

Map value

2022-06-26 13:12:00 shmilyhq

main Method

 public static void main(String[] args) {
    
        try {
    
            String json = "[
            {
    
                "user1":{
    
                "12":"age1",
                "23":"age2",
                "34":"age3"}
            },
            {
    
                "user2":{
    
                "90":"scrouse1",
                "87":"scrouse2",
                "98":"scrouse3"}
         }]";
            JSONArray jsonArray = JSONArray.parseArray(json);
            for (Object object : jsonArray) {
    
                Map<String, Object> map = JSONObject.parseObject(JSON.toJSONString(object));
            } catch(Exception e){
    

            }
        }
    }

demo1 according to entrySet() Get all using iterators value value ( Higher performance )

        private void handler (Map < String, String > map){
    
            Iterator<Map.Entry<String, Object>> entryIt = map.entrySet().iterator();
            while (entryIt.hasNext()) {
    
                Map.Entry<String, Object> entity = entryIt.next();
                Map<String, String> entityValue = (Map<String, String>) entity.getValue();
                Iterator<Map.Entry<String, String>> entryIt = entityValue .entrySet().iterator();
                while (entryIt.hasNext()) {
    
                    Map.Entry<String, String> entity = entryIt.next();
                    String val = entity.getValue();
                    String busType = entity.getKey();
                    System.out.println(busType + val);
                }
            }

        }

demo2

        private void handler (Map < String, String > map){
    
			for(String k : map.keySet()){
    
			  Map<String, String> entityValue = (Map<String, String>) map.get(k);
			  for(String k2 : entityValue .keySet()){
    
			   System.out.println(k2+ entityValue .get(k2));
				}
			}
        }

The following two are not recommended

demo3 adopt Map.entrySet Traverse key and value, Recommended for large capacity

private void handler (Map < String, String > map){
    
			for(Map.Entry<String, String> entry : map.entrySet()){
    
			  Map<String, String> entityValue = (Map<String, String>) map.getValue;
			  for(Map.Entry<String, String> entry : entityValue.entrySet()){
    
			   System.out.println(k2+ entityValue .get(k2));
				}
			}
        }

demo4 according to keySet() Get all using iterators value value ( Low performance )

private void handler (Map < String, String > map){
    
		Iterator<String> it = map.keySet().iterator();
			while(it.hasNext()){
    
			  String key = it.getKey();
			  Map<String, String> entityValue = (Map<String, String>) map.get(key );
			  Iterator<String> it2 = entityValue .keySet().iterator();
			  while(it2 .hasNext()){
    
			  String key2 = it2.getKey();
			   System.out.println(key2 + it2.get(key2 ));
				}
			}
        }

Reprint https://blog.csdn.net/qq_19694251/article/details/113524049

原网站

版权声明
本文为[shmilyhq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261222578639.html