zoukankan      html  css  js  c++  java
  • 【方法2】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录

    依据guigui111111的建议:先把Map按Key从大到小排序,然后再把Key和Value互换。这也是一种非常好的思路,我写了一下代码,顺便贴上来,供大家參考与分享。


    package shuai.study.map;
    
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.TreeMap;
    
    /**
     * @author shengshu
     * 
     */
    public class UniqueMap1 {
    
    	// Transfer to sorted Map
    	public static Map<String, String> transferToSortedMap(Map<String, String> map) {
    		// Define comparator for TreeMap
    		// Note: Sort according to descending, because retain the smaller Key's record when exchanging Map's Key and Value
    		Map<String, String> sort_map = new TreeMap<String, String>(new Comparator<String>() {
    			@Override
    			public int compare(String key1, String key2) {
    				return key2.hashCode() - key1.hashCode();
    			}
    		});
    
    		sort_map.putAll(map);
    
    		return sort_map;
    	}
    
    	// Exchange Map's Key and Value
    	public static Map<String, String> exchangeMap(Map<String, String> map) {
    		Map<String, String> exchange_map = new TreeMap<String, String>();
    
    		for (String key : map.keySet()) {
    			String value = map.get(key);
    
    			exchange_map.put(value, key);
    		}
    
    		return exchange_map;
    	}
    
    	// Print Map
    	public static void printMap(Map<String, String> map) {
    		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
    
    		while (iterator.hasNext()) {
    			Entry<String, String> entry = iterator.next();
    
    			String key = entry.getKey();
    			String value = entry.getValue();
    
    			System.out.println(key + " --> " + value);
    		}
    	}
    
    	public static void main(String[] args) {
    		Map<String, String> map = new HashMap<String, String>();
    		map.put("A", "1");
    		map.put("C", "3");
    		map.put("D", "2");
    		map.put("B", "3");
    		map.put("E", "3");
    
    		// Sort Map by descending order
    		// Note: Sort according to descending, because retain the smaller Key's record when exchanging Map's Key and Value
    		Map<String, String> sort_map = UniqueMap1.transferToSortedMap(map);
    
    		// Exchange Key and Value for overlapping repetition record
    		Map<String, String> exchange_map = UniqueMap1.exchangeMap(sort_map);
    
    		// Exchange Map for recovering Key and Value, this Map is what we want
    		exchange_map = UniqueMap1.exchangeMap(exchange_map);
    
    		// Print Map
    		UniqueMap1.printMap(exchange_map);
    	}
    }
    


  • 相关阅读:
    IOS之推送通知(本地推送和远程推送)
    IOS,苹果内购和添加广告
    CSS3选择器、背景、边框、文本
    CSS2D旋转、过渡、动画
    JavaScript Array、Date、String
    那些不常用却很有的CSS
    纯CSS打造兼容各种浏览器的几何图形
    安装 SQLManagementStudio_x86_CHS(SQL Server Management Studio) 老提示重启的解决办法
    关于使用Html.RenderPartial和Html.Partial显示分部视图时提示参数错误的BUG
    学习从实践开始之jQuery插件开发:对话框插件开发
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4218149.html
Copyright © 2011-2022 走看看