TreeSet+LinkedHashSet+Comparable+Hashcode+Equals

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class TestTreeSet {

 public static void main(String[] args) {
  // reSortTreeset();
  reSortLinkedHashSet();
 }

 private static void reSortTreeset() {
  Set<People> tempTs = new TreeSet<People>();
  tempTs.add(new People("baa", 2));
  tempTs.add(new People("abc", 1));
  tempTs.add(new People("acb", 1));
  tempTs.add(new People("bba", 1));
  tempTs.add(new People("aaa", 1));
  tempTs.add(new People("aaa", 2));
  tempTs.add(new People("baa", 1));

  Iterator<People> it = tempTs.iterator();
  while (it.hasNext()) {
   People mes = (People) it.next();
   System.out.println(mes.name + " " + mes.age);
  }

 }

 private static void reSortLinkedHashSet() {
  LinkedHashSet<People> tempTs = new LinkedHashSet<People>();
  tempTs.add(new People("baa", 2));
  tempTs.add(new People("abc", 1));
  tempTs.add(new People("acb", 1));
  tempTs.add(new People("bba", 1));
  tempTs.add(new People("aaa", 1));
  tempTs.add(new People("aaa", 2));
  tempTs.add(new People("baa", 1));

  Iterator<People> it = tempTs.iterator();

  while (it.hasNext()) {
   People mes = (People) it.next();

   System.out.println(mes.name + " " + mes.age);
  }
 }

}

class People implements Comparable {
 People(String name, int age) {
  this.name = name;
  this.age = age;
 }

 String name;
 int 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;
 }

 int i = 0;

 @Override
 public int hashCode() {
  int x = 3;
  int y = 5;
  for (int i = 1; i < 5; i++) {
   x = x * x + y;
   y = x + y;
  }
  return x * y;
 }

 public boolean equals(Object obj) {
  if (!(obj instanceof People)) {
   return false;
  }
  if (obj == null) {
   return false;
  }
  if (this == obj) {
   return true;
  }
  if (this.getAge() == ((People) obj).getAge()) {
   if (this.getName().equals(((People) obj).getName())) {
    return true;
   }
   return false;
  }
  return false;
 }

 public String toString() {
  return name + " " + String.valueOf(age);
 }

 // 覆盖Comparable接口的compareTo方法
 @Override
 public int compareTo(Object o) {
  if (this.getName().compareTo(((People) o).getName()) > 0) {
   return 1;
  }
  if (this.getName().compareTo(((People) o).getName()) < 0) {
   return -1;
  }

  return this.getAge() - ((People) o).getAge();

 }

}
如果People类不实现Comparable接口中的compareTo()方法则会报错:

Exception in thread "main" java.lang.ClassCastException: People cannot be cast to java.lang.Comparable
 at java.util.TreeMap.put(TreeMap.java:542)
 at java.util.TreeSet.add(TreeSet.java:238)
 at TestTreeSet.reSortTreeset(TestTreeSet.java:16)
 at TestTreeSet.main(TestTreeSet.java:9)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值