使用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() {
ListstuList = new ArrayList();
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() {
MaphashMap = new HashMap();
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
* set=hashMap.keySet(); for(Student stu:set){ System.out.println(stu);
* }
*/
System.out.println("the treeMap out put----");
TreeMaptreeMap = new TreeMap(
hashMap);
Setset2 = treeMap.keySet();
for (Student stu : set2) {
System.out.println(stu);
}
// map sort which implements the Comparator Interface
public static void mapKeySetSort2() {
MaphashMap = new HashMap();
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
* set=hashMap.keySet(); for(Teacher tea:set){ System.out.println(tea);
* }
*/
System.out.println("the treeMap out put----");
TreeMaptreeMap = new TreeMap(
new TeacherComparator());
treeMap.putAll(hashMap);
Setset2 = treeMap.keySet();
for (Teacher tea : set2) {
System.out.println(tea);
}
public static void sortValues() {
Mapkeyfreqs = new HashMap();
keyfreqs.put("w", 1);
keyfreqs.put("b", 2);
ArrayList> l = new ArrayList>(
keyfreqs.entrySet());
Collections.sort(l, new Comparator>() {
public int compare(Map.Entryo1,
Map.Entryo2) {
return (o1.getValue() - o2.getValue());
}
});
for (Entrye : 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{
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{
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.getId()-o2.getId();
}
TreeMap本身使用红黑树算法实现。