利用TreeMap對map進行排序

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'}
發佈了80 篇原創文章 · 獲贊 25 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章