Java中使用TreeMap将Map的KeySet排序

    使用TreeMap简单地HashMap的key进行排序,使用ArrayList对HashMap的value排序。

分别使用Comparator接口和Comparable接口实现key的排序。

package com.inspiration.examples.collection.sort;

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

public class SortExample {
	public static void listSort() {
		List<Student> stuList = new ArrayList<Student>();
		Student s1 = new Student(1, "wang");
		Student s2 = new Student(2, "li");
		stuList.add(s1);
		stuList.add(s2);
		Collections.sort(stuList);
		for (Student stu : stuList) {
			System.out.println(stu);
		}
	}

	// map sort which implements the Comparable Interface
	public static void mapKeySetSort() {
		Map<Student, Integer> hashMap = new HashMap<Student, Integer>();
		for (int i = 0; i < 100; i++) {
			Student stu = new Student(i, "wang" + i);
			hashMap.put(stu, i);
		}
		/*
		 * System.out.println("the hashMap out put----"); Set<Student>
		 * set=hashMap.keySet(); for(Student stu:set){ System.out.println(stu);
		 * }
		 */
		System.out.println("the treeMap out put----");
		TreeMap<Student, Integer> treeMap = new TreeMap<Student, Integer>(
				hashMap);
		Set<Student> set2 = treeMap.keySet();
		for (Student stu : set2) {
			System.out.println(stu);
		}
	}

	// map sort which implements the Comparator Interface
	public static void mapKeySetSort2() {
		Map<Teacher, Integer> hashMap = new HashMap<Teacher, Integer>();
		for (int i = 0; i < 100; i++) {
			Teacher tea = new Teacher(i, "wang" + i);
			hashMap.put(tea, i);
		}
		/*
		 * System.out.println("the hashMap out put----"); Set<Teacher>
		 * set=hashMap.keySet(); for(Teacher tea:set){ System.out.println(tea);
		 * }
		 */
		System.out.println("the treeMap out put----");
		TreeMap<Teacher, Integer> treeMap = new TreeMap<Teacher, Integer>(
				new TeacherComparator());
		treeMap.putAll(hashMap);
		Set<Teacher> set2 = treeMap.keySet();
		for (Teacher tea : set2) {
			System.out.println(tea);
		}
	}
	
	public static void sortValues() {
		Map<String, Integer> keyfreqs = new HashMap<String, Integer>();
		keyfreqs.put("w", 1);
		keyfreqs.put("b", 2);
		ArrayList<Entry<String, Integer>> l = new ArrayList<Entry<String, Integer>>(
				keyfreqs.entrySet());
		Collections.sort(l, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> o1,
					Map.Entry<String, Integer> o2) {
				return (o1.getValue() - o2.getValue());
			}
		});
		for (Entry<String, Integer> e : l) {
			System.out.println(e.getKey() + "::::" + e.getValue());
		}
	}

	public static void main(String[] args) {
		// listSort();
		// System.exit(1);
		// mapSort();
//		mapKeySetSort2();
	}

}
package com.inspiration.examples.collection.sort;

public class Student implements Comparable<Student>{
	private int id;
	private String name;
	public Student(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public int compareTo(Student o) {
//		return o.id-this.id;
		return this.id-o.id;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}

}
package com.inspiration.examples.collection.sort;

public class Teacher {
	private int id;
	private String name;
	public Teacher(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Teacher other = (Teacher) obj;
		if (id != other.id)
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
	
}
package com.inspiration.examples.collection.sort;

import java.util.Comparator;

public class TeacherComparator implements Comparator<Teacher>{
	@Override
	public int compare(Teacher o1, Teacher o2) {
		return o1.getId()-o2.getId();
	}

}
TreeMap本身使用红黑树算法实现。

你可能感兴趣的:(Map排序,HashMap的key排序)