Java入门需要了解(常用集合Set-三十一)

集合Set

Set的特点

- 其中的元素不能重复(set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2)
- 无序
- 其中的方法和Collection一样

Set的常用方法

  • Set中的方法和Collection中的方法一样

List的常用子类

HashSet

  • HashSet实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。不保证 set 的获取顺序
  • 它不保证该顺序不变
  • 此类允许使用 null 元素
  • 不同步
  • 无序eg:
        HashSet hashSet=  new HashSet();
        hashSet.add("d111");
        hashSet.add("d2222");
        hashSet.add("d3333");
        hashSet.add("d44");
        Iterator it = hashSet.iterator();
        while (it.hasNext()){
            System.out.println("it = " + it.next());
        }

打印结果:
it = d111
it = d44
it = d2222
it = d3333
结果是无序的

  • 不重复eg:
HashSet hashSet=  new HashSet();
        hashSet.add("d111");
        hashSet.add("d2222");
        hashSet.add("d3333");
        hashSet.add("d44");
        hashSet.add("d44");
        hashSet.add("d44");
        hashSet.add("d44");
        Iterator it = hashSet.iterator();
        while (it.hasNext()){
            System.out.println("it = " + it.next());
        }

打印结果为:
it = d111
it = d44
it = d2222
it = d3333

  • HashSet的好处是取出元素非常快,因为他不需要遍历集合
  • 元素不能重复

在HasSet中存内容的时候,先判断Hash值知否相同,如果相同,则再进行equals判断,如果两者都相同,则视为两个对象为同一个对象。
如果Hash值不同,则不进行equals判断的.

  • HashSet存储内容时eg:

import java.util.HashSet;
import java.util.Iterator;

class HashSetDemo2 {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Person("dd1",21));
        hashSet.add(new Person("dd2",22));
        hashSet.add(new Person("dd3",21));
        hashSet.add(new Person("dd3",21));
        hashSet.add(new Person("dd4",21));
        Iterator it = hashSet.iterator();
        while (it.hasNext()) {
            Person p = (Person)it.next();
            System.out.println(p);
        }
    }
}

/**
 * 测试对象
 */
class Person {

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        System.out.println("--------存的时候调用HashCode");
        return this.name.hashCode() + this.age;
    }

    @Override
    public boolean equals(Object obj) {
        Person p = (Person)obj;
        System.out.println("=========如果HashCode相同,则判断内容");
        return this.name.equals(p.getName()) && this.age == p.getAge();
    }

    @Override
    public String toString() {
        return "name=" + this.name + ",age=" + this.age;
    }

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

LinkedHashSet(有序的HashSet)

有可预知迭代顺序的 Set 接口实现
保证唯一

TreeSet

  • 可以对集合中的元素进行排序
  • 非同步
  • 判断元素唯一性的方式:存储在TreeSet集合中的元素,需要实现Comparable接口,并且判断唯一性的方法就是看:Comparable.compareTo(T o)方法,如果该方法返回0,则表示同一个对象,不予存储。
  • TreeSet代码示例:

import java.util.Iterator;
import java.util.TreeSet;

class TreeSetTest2 {
    public static void main(String[] args) {

        TreeSet treeSet = new TreeSet();
        treeSet.add(new Person("aaa",21));
        treeSet.add(new Person("bbb",23));
        treeSet.add(new Person("ddd",18));
        treeSet.add(new Person("ccc",21));
        Iterator it = treeSet.iterator();
        while (it.hasNext()) {
            System.out.println("it = " + it.next());
        }
    }
}

class Person implements Comparable {
    private String name;
    private int age;

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=" + this.name + ",age=" + this.age;
    }

    @Override
    public int compareTo(Object o) {
        Person p = (Person)o;
        int com = this.age - p.age;
        return com == 0 ? 0 : this.name.compareTo(p.name);
    }

    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;
    }
}
  • 如果对象不具备自然排序(即没有实现Comparable接口),则让集合自身具备比较功能,适用这样的构造器TreeSet(Comparator<? super E> comparator),直接创建一个TreeSet实例,Comparator可以隐式或者显示方式定义
  • 自定义比较器实现排序:

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

class TreeSetTest3 {
    public static void main(String[] args) {

        //年龄优先排序,使用自定义的比较器实现TreeSet排序
        TreeSet treeSet = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1,Object o2) {
                Person p1 = (Person)o1;
                Person p2 = (Person)o2;
                int com = p1.getName().compareTo(p2.getName());
                return com != 0 ? com : p1.getAge() - p2.getAge();
            }
        });
        treeSet.add(new Person("bbb",23));
        treeSet.add(new Person("ddd",18));
        treeSet.add(new Person("aaa",21));
        treeSet.add(new Person("ccc",21));
        Iterator it = treeSet.iterator();
        while (it.hasNext()) {
            System.out.println("it = " + it.next());
        }
    }
}

class Person2 {
    private String name;
    private int age;

    public Person2(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=" + this.name + ",age=" + this.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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值