zoukankan      html  css  js  c++  java
  • TreeMap和TreeSet简单应用

     建一个实体类并实现Comparable接口重写compareTo方法

    public class pojo implements Comparable<pojo> { 
            private int age;
            private String name;
            public pojo() {
                // TODO Auto-generated constructor stub
            }
            public pojo(String name, int age) {
                super();
                this.name = name;
                this.age = age;
            }
            public int getAge() {
                return age;
            }
            public String getName() {
                return name;
            }
            public void setAge(int age) {
                this.age = age;
            }
            public void setName(String name) {
                this.name = name;
            }
            @Override
            public int compareTo(pojo o) {
                // TODO Auto-generated method stub compareTo方法需要明确的返回1 或0或 -1;
     return this.age-o.getAge()>0?1:(this.age-o.getAge()<0?-1:0); }

    TrreSet演示 排序是根据pojo类中的age属性进行的,直接上了main方法,注意:如果不在类中重写

    compareTo方法在使用set添加时会报类型转换错误,或者写一个工具类实现Comparator<T>接口也可以。

    public static void main(String[] args) {
    			pojo p1=new pojo("p1",45);
    			pojo p2=new pojo("p1",55);
    			pojo p3=new pojo("p1",75);
    			pojo p4=new pojo("p1",35);			
    			Set<pojo> trreset=new TreeSet<pojo>();
    			trreset.add(p1);
    			trreset.add(p2);
    			trreset.add(p3);
    			trreset.add(p4);
    			for (pojo pojo : trreset) {
    				System.out.println(pojo.getAge()+"--"+pojo.getName());
    			}}
    

      TreeMap演示 对键进行排序 方式TreeSet一样,都是实现了接口

    public static void main(String[] args) {
    			pojo p1=new pojo("p1",45);
    			pojo p2=new pojo("p1",55);
    			pojo p3=new pojo("p1",75);
    			pojo p4=new pojo("p1",35);			
    			Map<pojo,String> map=new TreeMap<pojo,String>();
    			map.put(p4, "jfle");
    			map.put(p3, "jfle");
    			map.put(p2, "jfle");
    			for (Map.Entry<pojo,String> string : map.entrySet()) {
    				System.out.println(string.getKey().getAge());
    			}
    }
    

      总结  虽然java提供了一些集合和数组的排序,但是如果想根据自己的规则进行排序的话,就需要这样实现,比如TreeSet里放的是引用数据

    类型的话,如果不实现那两个接口也无法添加(会报类型转换错误),想根据自己的规则排序,就需要实现那两个接口重写方法。

    如果是hashMap或者这是Hashset需要排序,则需要先转为list,进行排序,如果类中实现了Comparable接口,就在srot方法中new Comparable

    如果写的工具类实现了 Comparator 接口,那么就在sort方法中new Comparator。

    //这里将map.entrySet()转换成list
            List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
            //然后通过比较器来实现排序
            Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
                //升序排序
                public int compare(Entry<String, String> o1,
                        Entry<String, String> o2) {
                    return o1.getValue().compareTo(o2.getValue());
                }
     
            });
    

      

  • 相关阅读:
    [转载] 关于mkvtoolnix批量处理的
    转载:JMeter压力测试入门教程[图文]
    分享 stormzhang的Andoid学习之路
    Sublime Text 2 插件
    PHP 操作SQLite
    curl 远程下载图片
    centos lamp 配置
    php 例子 如何转换ISO8601为 utc时间
    php 常用 常量集合
    php 文档操作
  • 原文地址:https://www.cnblogs.com/sanduweiliangxtx/p/6019334.html
Copyright © 2011-2022 走看看