黑马程序员——Set集合+hashCode+比较器

 ——- android培训java培训、期待与您交流! ———-

1.Set集合
Se接口方法和父接口Collection一样
* Set接口派系特点
不允许存储重复元素
无序集合,不保证迭代顺序
没有索引

Set集合迭代方式,迭代器,增强for

  /*
    Set集合存储字符串并迭代
    使用Set接口实现类 HashSet进行演示
    接口和实现类多态调用,带泛型
  */
public static void method(){
        Set<String> set = new HashSet<String>();
        set.add("egsd");
        set.add("fd");
        set.add("wedf");
        set.add("fd");

        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        for(String s : set){
            System.out.println(s);
        }
    }

2.HashSet
底层是哈希表结构,查询速度快
无序集合,没有索引,不重复,允许存储null
HashSet集合是线程不安全的集合,运行速度快
HashSet底层数据结构哈希表,存储到HashSet集合中的对象,必须实现hashCode和equals方法,保证唯一性

  • 对象的哈希值
    JAVA中,每一个类的对象,都具有一个十进制数
    十进制数,叫做这个对象的哈希值

    class Object{ public native int hashCode(); }
    所有类都是他的子类,每一个都有方法hashCode()

    对象的哈希值,就是JVM根据对象所在的内存,通过自己底层计算方法
    给这个对象计算出一个十进制数,数就是哈希值
    数值,程序一个参考而已,没有内存任何因素

    哈希值就是一个普通十进制数,出现目的就是为了哈希表而来
    使用价值 集合中的哈希表
    String extends Object { public int hashCode(){} }
    重写了父类方法hashCode
    自己对字符串进行哈希值的计算
    abc字符串对象自己的哈希值 96354
    String类自己计算和,父类Object中的hashCode方法无关

public class Student {
    private String name;
    private int age;


    /*
     * 重写hashCode方法
     * 姓名,年龄不同,但是恰好计算的哈希值一样,降低概率
     * 哈希值是我们自己定义的
     */
    public int hashCode(){
        return name.hashCode() + age*39;
    }
    public boolean equals(Object obj){
        if(obj == null)
            return false;
        if(this == obj)
            return true;
        if(obj instanceof Student){
            Student s = (Student)obj;
            return this.name.equals(s.name) && this.age==s.age;
        }
        return false;
    }

HashSet集合案例
存储6个对象,姓名,年龄一样,看成是同一个对象
思想:集合存储的依据是哈希值,让Student中的姓名和年龄一样的同学,得到一样的哈希值

public class HashSetDemo {
    public static void main(String[] args) {
        HashSet<Student> set = new HashSet<Student>();
        set.add(new Student("a",19));
        set.add(new Student("b",20));
        set.add(new Student("c",18));
        set.add(new Student("c",18));
        set.add(new Student("d",17));
        set.add(new Student("e",22));

        for(Student s : set){
            System.out.println(s);
        }

3.TreeSet
红黑数结构,存储对象排序
线程不安全集合,运行速度快
排序方式
– 依靠对象自然顺序
实现 类实现Comparable接口,重写compareTo方法
对象具备自然顺序
java.lang.Comparable 接口,实现这个接口,强制排序,排序被成为对象的自然顺序

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public  int compareTo(Student s) {
        int num = this.name.compareTo(s.name);
        return num==0?this.age-s.age:num;
    }
}

– TreeSet自己使用比较器
实现 类实现Comparator接口,重写compare方法
创建TreeSet集合,比较器对象传递到TreeSet构造方法
比较器优先

class MyComparator implements Comparator<Student>{
    public int compare(Student s1,Student s2){
        int age =s1.getAge() - s2.getAge();
        return age == 0?s1.getName().compareTo(s2.getName()):age;     
    }
}

在使用自定义比较器时只需将比较器对象传递到TreeSet构造方法
TreeSet ts = new TreeSet( new MyComparator());

  • TreeSet的使用
public static void main(String[] args) {
    TreeSet<Student> ts = new TreeSet<Student>();
    ts.add(new Student("b",19));
    ts.add(new Student("c",20));
    ts.add(new Student("aa",20));
    ts.add(new Student("c",20));
    ts.add(new Student("a",18));
    ts.add(new Student("a",20));

    for(Student s : ts){
        System.out.println(s);
    }
}

Set集合+hashCode+比较器这天课程的总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值