ITeye博客

Map排序

mlaaalm · 2010-06-20 01:58 · Collection · 非技术 · C · Java · C++ · C# · Markdown

package samples;

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

public class SortHashMap {
	public static void main(String[] args){
		String input= "1122334455667788990";
		
		input=input.replaceAll(" ", "");
		
		HashMap<String,Integer> hmString = new HashMap<String,Integer>();
		
		for(char c:input.toCharArray()){
			String key = c+"";
			if(hmString.containsKey(key)){
				hmString.put(key, hmString.get(key)+1);
			}else{
				hmString.put(key, 1);
			}
			
		}
		
		populateMap(hmString);
		
		
		Integer max = Collections.max(hmString.values());
		
		for(Map.Entry<String, Integer> entry: hmString.entrySet()){
			if (entry.getValue().equals(max)){
				System.out.print(entry.getKey() + ":");
				System.out.print(entry.getValue() + "|");
			}
		}
	}
	
	public static void populateMap(Map<String, Integer> c) {
		for (Map.Entry<String, Integer> entry : c.entrySet()) {
			System.out.print(entry.getKey() + ":");
			System.out.print(entry.getValue() + "|");
		}
		System.out.println();
	}

}

 

 

 

package samples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 *
 */

public class Test {

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("ee", 3);
        map.put("b", 1);
        map.put("d", 2);
        map.put("eee", 3);
        map.put("A", 1);
        map.put("K", 2);
        map.put("ade", 1);
        map.put("c", 2);
        map.put("aee", 3);
        map.put("a", 1);
        map.put("faed", 2);
        map.put("bdd", 1);
        map.put("qec", 2);
        map.put("eade", 3);
        map.put("Aadf", 1);
        map.put("Kqe", 2);

        Map<String, Integer> sortMap = new Test().sortMap(map);

        for(Map.Entry<String, Integer> entry : sortMap.entrySet()) {
            System.out.println(entry.getKey() + " --> " + entry.getValue());
        }
    }

    /**
     * 返回排序完成的Map
     * @return
     */
    public <K, V extends Number> Map<String, V> sortMap(Map<String, V> map) {
    	/*
    	 * 自定义MyMap,封装key/Value
    	 */
        class MyMap<M, N> {
            private M key;
            private N value;
          
            private M getKey() {
                return key;
            }
            private void setKey(M key) {
                this.key = key;
            }
            private N getValue() {
                return value;
            }
            private void setValue(N value) {
                this.value = value;
            }
        }
        /*
         * 自定义的myMap放入list,自定义对象有key ,value两个值
         */
        List<MyMap<String, V>> list = new ArrayList<MyMap<String, V>>();
        for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
            MyMap<String, V> my = new MyMap<String, V>();
            String key = i.next();
            my.setKey(key);
            my.setValue(map.get(key));
            list.add(my);
        }

        /*
         * 对list进行排序,根据value
         * 如果value相等,根据key
         */
        Collections.sort(list, new Comparator<MyMap<String, V>>() {
            public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
                if(o1.getValue() == o2.getValue()) {
                    return o1.getKey().compareTo(o2.getKey());
                }else{
                    return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
                }
            }
        });

        /*
         * 排序的list写入LinkedHashMap,保证顺序
         */
        Map<String, V> sortMap = new LinkedHashMap<String, V>();
        for(int i = 0, k = list.size(); i < k; i++) {
            MyMap<String, V> my = list.get(i);
            sortMap.put(my.getKey(), my.getValue());
        }
        return sortMap;
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。