set接口和它的实现类treeset和hashset

第一:set接口:set接口里面的元素不能重复,因此null也只能有一个。

第二:treeset实现类:需要排序(有序)

1.间接实现了set接口(Treeset继承了AstractSet这个类,AstractSet类实现了set接口)。

2.对保存的元素进行排序(默认情况下调用comparable接口中的comperTo(比较的)方法,也就是默认排序,继承之后就能实现添加了),在我们排序的时候需要指定排序的规则,(而这个规则定义在comparable接口中,因此Treeset集合里的对象要实现这个接口), (例如:我指定的规则是比较学生对象里的年龄,则只需要两个对象的年龄一样,就认为这两个对象是痛一个对象(认为他们的内容一样)。这里是为了排序,按照年龄排序。)(list接口是重写equals方法,因此要求全部内容一样才算一样。set接口重写的是comperTo方法, 只需两个对象的年龄一样就会认为是痛一个对象(认为他们的内容一样))。排序的结果是放进去的顺序跟取出来的顺序不一样
 3.comparable这个接口是排序的意思,这个接口里面有比较大小的方法compareTo,当实现类实现这个接口的时候也就是重写了这个比较的方法。

4.TreeSet是线程不同步,不安全的。

5.iterator迭代器,这也是个接口。 set需要借助迭代器来遍历,遍历的时候不知道循环次数,所以用while(在list接口的时候,是因为知道元素下标,因此能用for循环,set不知道容器里面元素的具体位置,因此要借助迭代器)
 6.TreeSet的底层是个二叉树(右节点比跟节点要大,左节点比跟节点要小)

7.代码解释:

public class Student implements Comparable{
 private int age;
 private double hight;
 private String name;
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public double getHight() {
  return hight;
 }
 public void setHight(double hight) {
  this.hight = hight;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Student() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Student(int age, double hight, String name) {
  super();
  this.age = age;
  this.hight = hight;
  this.name = name;
 }
 //这里的tostring本来是没有什么用处的,在这里重写tostring是为了在控制台查看运行结果
 @Override
 public String toString() {
  return "Student [age=" + age + ", hight=" + hight + ", name=" + name
    + "]";
 }
 //比较Treeset集合中的元素。如果两个对象的age和hight都相等我们就认为这俩个对象是同一个元素。这是通过age进行比较,如果两个对象只有age相等,则会按添加顺序排列所有年龄相等的对象。如果age不相等就直接按照age进行排序了
 //比较的方法1:这个方法相当于把equals方法和比较的方法写在了一起
/* @Override
 public int compareTo(Object o) {
   Student stu=(Student)o;
   if(this.age==stu.age&&this.hight==stu.hight&&this.name.compareTo(stu.name)==0){
      return 0;
     }else
      if(this.age>stu.age){
      return 1;        
     }else{ 
     return -1;
     } 
 }
 */
 //方法2:这个是把compareTo方法与equals方法分开写。注意要保证两个方法中属性(个数)的一致性
 public int compareTo(Object o) {
      Student stu=(Student)o;
      if(this.age>stu.age){
       return 1;
      }else
      if(this.age<stu.age){
       return -1;
      }
     
      if(this.hight>stu.hight){
       return 1;
      }else
      if(this.hight<stu.hight){
       return -1;
      }
     
      if(this.name.compareTo(stu.name)>0){
       return 1;
      }else
      if(this.name.compareTo(stu.name)<0){
       return -1;
      }
      return 0;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (!(obj instanceof Student))
   return false;
  Student other = (Student) obj;
  if (age != other.age)
   return false;
  if (Double.doubleToLongBits(hight) != Double
    .doubleToLongBits(other.hight))
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  return true;
 }
}

 

public class TreeSetTest {
 /**
  *
  *  @方法的作用:使用TreeSet 保存学生,使学生按照年龄和身高排序,并遍历集合,使用两种方式 实现学生排序功能
  */
 public static void main(String[] args) {
  Student stu1=new Student(13,16,"张三");
  Student stu2=new Student(16,16,"liss");
  Student stu3=new Student(19,14.6,"李四");
  Student stu4=new Student(13,16.8,"王五");
  Student stu5=new Student(32,19,"主流");
  Student stu6=new Student(32,19,"主流");
  Student stu7=new Student(32,10,"haha");
  TreeSet ts=new TreeSet();
  ts.add(stu1);//在add的时候默认调用compareTo方法
  ts.add(stu2);
  ts.add(stu3);
  ts.add(stu4);
  ts.add(stu5);
  ts.add(stu6);
  ts.add(stu7);
  Iterator it=ts.iterator();
  while(it.hasNext()){
   Object obj = it.next();
   System.out.println(obj);
  }
 }

}

第三:hashset实现类:不排序

1.底层是个哈希表(跟内存地址有关,每次运行的顺序可能会不一样,也就是产生这个对象的时候不一定每次都是这个内存地址)也就是Hashmap的实例。

2.也只能有一个null,因为他们都实现了set接口。

3.在new的时候,地址不一定总是同一个,因此在对内存中给Hashset分配好多桶,到时候添加元素的时候,是添加到某一个桶内。在查找的时候只需要找到这个桶,这样提高了效率,
 4.构造函数,空构造调用了map的空构造默认初始容量是16,也就是16个桶,加载因子是0.75倍。

5.在add方法中,如果集合中已经有这个元素了(有这个内容了),就不在添加了,就添加不进去了。

6.解决set:不允许重复的问题:但是我们在添加重复元素的时候能够添加重复元素,是以为我们没有重写equals方法,没重写的情况下是不比较内容的,因此写两个内容一样的对象也能添加成功。现在我们重写equals方法,但是重写之后发现内容一样的对象仍然能够添加成功。
 7.重写equals方法的之前还要重写hashcode方法,hashcode方法在不重写的时候表示内存地址,重写之后表示一个常量和一个变量经过运算后得到到整数值。重写hashcode是运算(hashcode方法里面是对对象的属性值进行运算比较)对象的每个属性。当两个对象的某个属性是相同的值(这两个对象的的hashcode相等)的时候才会调用equals方法进行判断。如果这两个对象相等就不添加了,如果不相等就放入一个桶中。hashcope方法决定对象在哪个桶中保存,如果两个对象的hashcope值一样(对象A的hashcope方法里面的属性值和对象B对应的hashcope方法的属性值一样),就把他们放入同一个桶中。
8.当重写hashcode方法的时候如果所有属性都用上了,这时候hashcode相等就等于equals方法,否则hashcode的范围小于equals。(两个对象的hashcope值一样,就是说明在hashcope方法的属性中,这两个对象的参与的属性是一样的。但是因为自己重写hashcope方法是时候未必把对象的每个属性都写进hashcope方法中,因此当对象的hashcope值一样的时候未必就是同一个对象)

9.删除和包含的方法现在不是只跟equals方法有关了,而是与hashcode和equals这两个有关

10:代码解释

public class Emp {
 private double salar;
 private String name;
 private int age;
 public double getSalar() {
  return salar;
 }
 public void setSalar(double salar) {
  this.salar = salar;
 }
 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 Emp() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Emp(double salar, String name, int age) {
  super();
  this.salar = salar;
  this.name = name;
  this.age = age;
 }

 @Override
 public String toString() {
  return "Emp [salar=" + salar + ", name=" + name + ", age=" + age + "]";
 }
 //迭代的时候先进入hashCode方法,看看每个对象放入哪个桶中(属性相同的对象会放入一个桶中),这个方法里面放的属性是salar
 

@Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  /*result = prime * result + age;
  result = prime * result + ((name == null) ? 0 : name.hashCode());*/
  long temp;
  temp = Double.doubleToLongBits(salar);
  result = prime * result + (int) (temp ^ (temp >>> 32));
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (!(obj instanceof Emp))
   return false;
  Emp other = (Emp) obj;
  if (age != other.age)
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  if (Double.doubleToLongBits(salar) != Double
    .doubleToLongBits(other.salar))
   return false;
  return true;
 }

}

 

public class HashSetTest {

 /**
  *  @方法的作用:使用HashSet 保存员工信息,使员工工资一样的存入一个桶中,并遍历集合
  */
 public static void main(String[] args) {
  
  HashSet hs=new HashSet();
  hs.add(new Emp(5550,"张三",30));
  hs.add(new Emp(1000,"lisi",30));
  hs.add(new Emp(5550,"李四",25));
  hs.add(new Emp(4800,"王五",37));
  hs.add(new Emp(3000,"hh",56));
  hs.add(new Emp(3000,"王八",56));
  hs.add(new Emp(3000,"王八",56));
   Iterator it=hs.iterator();
  while(it.hasNext()){ 
   Object obj=it.next();
   System.out.println(obj);  
  }
  
 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值