黑马程序员_温习 集合三 (个人笔记)摘要(HashSet---TreeSet)

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


摘要(HashSet---TreeSet)



Collection集合体系
|--List集合


|--Set集合
|--HashSet:底层哈希表,无序,不可重复,非同步,比较方式(先HashCode哈希表  后equals)
|--TreeSet:底层二叉树,可排序,不可重复,比较方式 Comparable


HashSet:底层数据结构是哈希表
|--判断元素唯一:通过HashCode和equals方法完成(如果hashCode相同,再判断equals)
|--判断元素是否存在,删除等操作也是通过HashCode和equals确定元素的


例:往HashSet集合中存入自定义对象,姓名和年龄相同视为同一个人(重复元素)


摘要一:HashSet集合内元素要复写hashCode方法,以实现元素唯一性判断
当hashCode判断出现相同元素时,再用equals判断,故equals也要复写
import java.utli.*;
class person 
{
private String name;
private int age;
person(String name,int age)
{
this.name = name;
this.age = age;
}
public int hashCode()//复写hashCode方法
{
return name.hashCode()+age*39;//哈希表就是数值
}
public boolean equals(Object obj)//若哈希表数值相同,则再判断equals
{
if(!(obj instanceof person))//是否是person类型
throw new RuntimeException("类型有误");
person p = (person)obj;//转回person类
return this.name.equals(p.name) && this.age == p.age;//特有比较方式,即姓名和年龄都相同返回 true
}
public String getname()
{
return this.name;


}
public int getage()
{
return this.age;


}


}


class HashSetText
{
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add(new person("zhang",20));
hs.add(new person("zhang",30));
hs.add(new person("li",20));
hs.add(new person("zhang",20));
Iterator it = hs.iterator();
while (it.hasNext())
{
person p = (person)it.next();
System.out.println(p.getname()+"----"+p.getage());
}
}
}


TreeSet集合:底层是二叉树
|--判断元素唯一性加排序:
|--元素自带:Comparable接口-->(实现compareTo方法用于比较)


|--指定类:Comparator(比较器)-->(实现compare方法用于比较)(常用)


两种比较方式都是返回int型值(大于零,小于零,等于零)来排序,一般在方法中定义两个
比较条件,当主要条件相同时,再比较次要条件


例:往TreeSet集合中存入自定义学生对象,并按照年龄排序
摘要一:TreSet集合存储元素时必须满足一个条件即
|--让元素实现Comparable接口(自带比较)

|--指定类实现Comparator接口(比较器)


摘要二:比较方法中最好写两个比较条件(当主要条件满足时,再判断次要条件)
如本题,若年龄相同,可设置次要条件姓名,若姓名也相同,则视为同一个元素


import java.util.*;


class Student implements Comparable//元素自身实现Comparable,即自带比较方式
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)//复写compareTo方法,设置比较条件
{
if(!(obj instanceof Student))//判断类型
throw new RuntimeException("类型有误");
Student s = (Student)obj;//转回Student型
if(this.age>s.age)
return 1;
if(this.age == s.age)//如果年龄相同则
return this.name.compareTo(s.name);//判断次要条件,即姓名(字符串中自带compareTo方法)
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}


class bijiao implements Comparator//自定义类实现Comparator(比较器),可以指定集合比较的方式, 注意和Comparable不同
{
public int compare(Object o1,Object o2)//复写compare方法,有两个参数  注意,和compareTo不同
{
if(!(o1 instanceof Student))
throw new RuntimeException("类型有误");
if(!(o2 instanceof Student))
throw new RuntimeException("类型有误");
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if(s1.getAge()==s2.getAge())//若年龄相同则
return s1.getName().compareTo(s2.getName());//比较姓名
return s1.getAge()-s2.getAge();
}
}


class TreeSetDemo
{
public static void main(String[] args)
{
//TreeSet ts = new TreeSet();//使用元素自带比较方式
TreeSet ts = new TreeSet(new bijiao());//使用比较器
ts.add(new Student("zhang",20));
ts.add(new Student("zhang",22));
ts.add(new Student("long",20));
ts.add(new Student("long",20));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"----"+stu.getAge());
}


}

}


---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值