Treemap是可以根据键对map进行排序的,注意:是根据键。
一般来讲,键可以使Integer或者是String,
但是也可以是对象,但是该对象的实现类必须实现Comparable<T>接口。
class mycompare implements Comparable<mycompare>{
private int age;
private String name;
public mycompare(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "people{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(mycompare o) {
return this.getAge()-o.getAge();
}
}
public class TreeMapAndSet {
public static void main(String []args){
Map<mycompare, Integer> map1 = new TreeMap<>();
map1.put(new mycompare(1,"first"),1);
map1.put(new mycompare(2,"second"),2);
map1.put(new mycompare(4,"fourth"),3);
map1.put(new mycompare(5,"fifth"),4);
map1.put(new mycompare(3,"third"),5);
for(Map.Entry<mycompare,Integer> entry :map1.entrySet()){
System.out.println("键:"+entry.getKey()+"-----值:"+entry.getValue());
}
}
}
输出:键:people{age=1, name='first'}-----值:1
键:people{age=2, name='second'}-----值:2
键:people{age=3, name='third'}-----值:5
键:people{age=4, name='fourth'}-----值:3
键:people{age=5, name='fifth'}-----值:4
想想用这样的方法对值进行排序就很简单了,再来一个HashMap即可。
另外,如果mycompare没有实现Comparable接口,也可以这样子写:
class mycompare{
private int age;
private String name;
public mycompare(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "people{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TreeMapAndSet {
public static void main(String []args){
TreeMap<mycompare, Integer> treeMap2 = new TreeMap<>(new Comparator<mycompare>(){
public int compare(mycompare o1, mycompare o2) {
return o1.getAge()-o2.getAge();
}
});
treeMap2.put(new mycompare(1,"first"),1);
treeMap2.put(new mycompare(2,"second"),2);
treeMap2.put(new mycompare(4,"fourth"),3);
treeMap2.put(new mycompare(5,"fifth"),4);
treeMap2.put(new mycompare(3,"third"),5);
for(Map.Entry<mycompare,Integer> entry :treeMap2.entrySet()){
System.out.println("键:"+entry.getKey()+"-----值:"+entry.getValue());
}
/*Map<mycompare, Integer> map1 = new TreeMap<>();
map1.put(new mycompare(1,"first"),1);
map1.put(new mycompare(2,"second"),2);
map1.put(new mycompare(4,"fourth"),3);
map1.put(new mycompare(5,"fifth"),4);
map1.put(new mycompare(3,"third"),5);
for(Map.Entry<mycompare,Integer> entry :map1.entrySet()){
System.out.println("键:"+entry.getKey()+"-----值:"+entry.getValue());
}*/
}
}
输出一样。
另外来一个直接对值进行排序的方法,也是不用实现comparator的:
class mycompare {
private int age;
private String name;
public mycompare(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "people{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TreeMapAndSet {
public static void main(String []args){
Map<Integer,mycompare> map = new TreeMap<>();
map.put(1,new mycompare(1,"fff"));
map.put(2,new mycompare(2,"fff"));
map.put(3,new mycompare(4,"fff"));
map.put(4,new mycompare(3,"fff"));
map.put(5,new mycompare(5,"fff"));
List<Map.Entry<Integer,mycompare>> list = new ArrayList<Map.Entry<Integer, mycompare>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, mycompare>>() {
@Override
public int compare(Map.Entry<Integer, mycompare> o1, Map.Entry<Integer, mycompare> o2) {
return o1.getValue().getAge()-o2.getValue().getAge();
}
});
/* for(int i=0;i<list.size();i++){
System.out.println("键:"+list.get(i).getKey()+" 值:"+list.get(i).getValue());
}*/
for(Map.Entry<Integer,mycompare> e:list){
System.out.println(e.getKey()+"----"+e.getValue());
}
}
}
输出:1----people{age=1, name='fff'}
2----people{age=2, name='fff'}
4----people{age=3, name='fff'}
3----people{age=4, name='fff'}
5----people{age=5, name='fff'}