一、TreeSet中的元素比较有两种方式

1、定义一个类,实现Comparable接口  复写的是comparato方法

2、定义一个类,实现Comparator接口,覆盖compara方法(此种方法是当集合内元素不具备比较或者所具有的比较不是想要的时候,另建立类来实现comparato接口,通过重写compara方法来编写自己想要的排序方式)

当两种排序都存在时,以比较器为主。

二叉树都是以 return 0,ba判断两个元素相等。

代码示例:

/*TreeSet底层数据结构:
 *
 *
 *
 *
 *
 *
 *
 *
 *
 * */

package 集合;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet  ts = new TreeSet(new MyCompar());//第二种方法传入的比较器

//如下是用匿名内部类的方式传入比较器

ts.add(new Person3("lisan01",18));
        ts.add(new Person3("lisan02",19));
        ts.add(new Person3("lisan03",13));
        ts.add(new Person3("lisan01",20));
        ts.add(new Person3("lisan02",20));
        ts.add(new Person3("lisan01",20));
        
        //要求:按照年龄从小到达排序
        Iterator it = ts.iterator();
        while(it.hasNext()){
            Person3 ps = (Person3)it.next();
            System.out.println(ps.getName()+"::"+ps.getAge());
        }
        
    }
}

//定义一个类,实现Comparable接口  复写的是comparato方法
class Person3 implements Comparable{//该接口强制让学生具备比较性
    private String name;
    private int age;
    
    public Person3() {
        // TODO 自动生成的构造函数存根
    }
    public Person3(String name,int age){
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    public int compareTo(Object obj) {
        
        if(!(obj instanceof Person3)){
            throw new RuntimeException("不是人的对象");
        }else{
            Person3 ps = (Person3)obj;
            if(this.age>ps.age){
                return 1;
            }else if(this.age==ps.age){
                return this.name.compareTo(ps.name);
            }else{
                return -1;
            }
        }
    }

//第二种方式:定义一个类,实现Comparator接口,覆盖compara方法

class MyCompar implements Comparator{
    //要求要按名字来排序
    public int compare(Object obj1, Object obj2) {
        Person3 p1 = (Person3)obj1;
        Person3 p2 = (Person3)obj2;
        int num = p1.getName().compareTo(p2.getName());
        if(num == 0){
            /*if(p1.getAge()>p2.getAge())
                return 1;
            if(p1.getAge() == p2.getAge())
                return 0;
            if(p2.getAge()<p2.getAge())
                return -1;*/
            //用整数的包装类来实现comparato
            return new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
                
        }
        return num;
    }
    
}

}

TreeSet的更多相关文章

  1. HashSet HashTable 与 TreeSet

    HashSet<T>类 HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet& ...

  2. 计算机程序的思维逻辑 (44) - 剖析TreeSet

    41节介绍了HashSet,我们提到,HashSet有一个重要局限,元素之间没有特定的顺序,我们还提到,Set接口还有另一个重要的实现类TreeSet,它是有序的,与HashSet和HashMap的关 ...

  3. HashSet,TreeSet和LinkedHashSet的区别

    Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...

  4. 集合 LinkedList、ArrayList、Set、Treeset

    LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...

  5. HashSet和TreeSet

    package com.wzy.list; import java.util.HashSet; import java.util.Iterator; import java.util.Set; imp ...

  6. Java 集合类 TreeSet、TreeMap

    TreeMap和TreeSet的异同: 相同点: TreeMap和TreeSet都是有序的集合,也就是说他们存储的值都是拍好序的. TreeMap和TreeSet都是非同步集合,因此他们不能在多线程之 ...

  7. JAVA中的TreeSet

    TreeSet简介 TreeSet是一个有序的集合,它的作用是提供一个有序的Set集合,它继承于AbstractSet抽象类实现了NavigableSet<E>, Cloneable, j ...

  8. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  9. 【皇甫】☀ TreeSet

    -Set: 元素是无序(存入和取出的顺序不一致),元素不可以重复 |-- HashSet: 底层数据结构是哈希表 HashSet是如何保证元素的唯一性的呢? 是通过元素的两个方法,hashCode和e ...

随机推荐

  1. Mac 编写oracle 连接脚本

    首先需要本地存有sqlplus命令, 如果没有则需要到官网下载 也可点击我进行下载 (解压 readme.txt 有安装配置说明): 在Oracle官网下载instant client for os ...

  2. win7系统下的飞秋发送文件失败问题

    飞秋发送文件失败这个问题大多数是由防火墙引起的1.检查windows自带的防火墙设置,在左侧的"允许程序通过windows防火墙"查看飞秋是否存在,不存在则增加之,公网.专网都勾选 ...

  3. IT路上爹爹装装遇到的坑

    1.中英文字符格式:UTF-8 有BOM和无BOM Notepad++ Edit-Plus emacs vim Sublime Text3 记事本 有,无 UTF-8+ #coding:utf-8   ...

  4. js最详细的基础,jquery 插件最全的教材

    一.Js的this,{},[] this是Javascript语言的一个关键字,随着函数使用场合的不同,this的值会发生变化.但是有一个总的原则,那就是this指的是调用的函数自己. { } 大括号 ...

  5. WinForm跨窗体传值

    1.另一窗体建公共变量listdataRow public List<DataGridViewRow> listdataRow = new List<DataGridViewRow& ...

  6. iOS动画效果和实现

    动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,咱们不需要自己编写绘制动画的代码,Core Animation提供了丰富的api来实现你需要的动画效果. UIKit只用UIView来展示 ...

  7. NoSQL与RDBMS的九点区别联系

    原文链接:http://blog.sina.com.cn/s/blog_5373fb0b0101ft8a.html     1 理解ACID与BASE的区别(ACID是关系型数据库强一致性的四个要求, ...

  8. PHP面试总结

    从8月15号来到北京一直到今天,一月有余.来的这段时间一直准备笔试面试,大大小小的公司,乱七八糟面了10多家,近期才安顿下来.面试的这段时间感觉自己成长了不少.初来到这个陌生的城市一脸茫然,不会乘地铁 ...

  9. 导出(Excel格式)

    poi导出,需要的poi jar包: 步骤一.两个工具类: 1.ExcelUtil.java package util; import java.awt.Color; import java.io.F ...

  10. Quart.NET实施参考

    参考 1.博客园: http://www.cnblogs.com/lzrabbit/archive/2012/04/13/2447609.html 2.官网:http://www.cnblogs.co ...