TreeSet中的排序方式有哪些-创新互联

本篇文章给大家分享的是有关TreeSet中的排序方式有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为象山企业提供专业的网站设计制作、网站设计象山网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

直接上代码:

package exercise1;

public class Person implements Comparable{
  private int id;
  private String name;
  public Person(int id, String name) {
    super();
    this.id = id;
    this.name = name;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String toString() {
    return "Person [id=" + id + ", name=" + name + "]";
  }
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + id;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Person other = (Person) obj;
    if (id != other.id)
      return false;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    return true;
  }
  public int compareTo(Object o) {
    if(o instanceof Person){
      Person p=(Person)o;
      return this.name.compareTo(p.name);
    }
    return 0;
  }
  
}

当前题目:TreeSet中的排序方式有哪些-创新互联
新闻来源:http://myzitong.com/article/dcojjs.html