Map four methods for obtaining key and value values, and sorting the elements in the map

There are four main ways to get the value of the map, and these four methods are divided into two categories:

One is to call the map.keySet() method to get the value of key and value,

The other type is to get the value through the map.entrySet() method,

The difference between the two is that the former mainly obtains the set of all keys first. When you need to query the value of the value, you need to query the value through the key.

The latter directly takes out the key-value pair of key and value, and only needs to query it once. For better performance, I think it is better to use map.entrySet(). For details, please refer to map.keySet() and The comparison of map.EntrySet() , the following will introduce the four traversal methods and the elements in the map are compared and sorted by key or value:

 

package com.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Test {

	public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("b","4");
		map.put("a","5");
		map.put("c","3");
		map.put("d","5");
		
		//Through the map.keySet() method
		//Method 1: By getting the value of the key, then get the value;
		/*for(String key : map.keySet()){
			String value = map.get(key);
			System.out.println(key+"  "+value);
		}*/
		//Use the iterator to get the key;
		/*Iterator<String> iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key=iter.next();
			String value = map.get(key);
			System.out.println(key+" "+value);
		}*/
		//Through the map.entrySet() method
		//Method 1: Loop each key-value pair in the map, and then get the key and value
		/*for(Entry<String, String> vo : map.entrySet()){
			vo.getKey();
			vo.getValue();
			System.out.println(vo.getKey()+"  "+vo.getValue());
		}*/
		
		/*//Use the iterator to get the key
		Iterator<Entry<String,String>> iter = map.entrySet().iterator();
		while(iter.hasNext()){
			Entry<String,String> entry = iter.next();
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+" "+value);
		}*/
		
		//Convert map<String,String> to ArryList, but the elements in the list are Entry<String,String>
		List<Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
		Collections.sort(list,new Comparator<Entry<String,String>>(){
			@Override
			public int compare(Entry<String, String> o1,
					Entry<String, String> o2) {
				int flag = o1.getValue().compareTo(o2.getValue());
				if(flag==0){
					return o1.getKey().compareTo(o2.getKey());
				}
				return flag;
			}
		});
		// Traverse the list to get the sorted elements in the map
		for(Entry<String, String> en : list){
			System.out.println(en.getKey()+" "+en.getValue());
		}
		
	}

}
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325326657&siteId=291194637