OrderedLinkedHashMap

package net.tqm.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class OrderedLinkedHashMap {

	public static LinkedHashMap getOrder(Map map) {
		List> infoIds = new ArrayList>(
				map.entrySet());

		// 排序
		Collections.sort(infoIds, new Comparator>() {
			public int compare(Map.Entry o1,
					Map.Entry o2) {
				String p1 = (String) o1.getKey();
				String p2 = (String) o2.getKey();

				return p1.compareTo(p2);
			}
		});

		/* 转换成新map输出 */
		LinkedHashMap newMap = new LinkedHashMap();

		for (Map.Entry entity : infoIds) {
			newMap.put(entity.getKey(), entity.getValue());
		}

		return newMap;
	}

}

你可能感兴趣的:(个人总结)