利用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'}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值