【Java Set接口】

78 篇文章 0 订阅

HashSet

package com.yuzhenc.collection;

import java.util.HashSet;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 20:51:34
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test12 {
    public static void main(String[] args) {
        //HashSet特点:唯一、无序
        HashSet<String> hashSet = new HashSet<>();
        System.out.println(hashSet.add("abc"));//true
        System.out.println(hashSet.add("def"));//true
        System.out.println(hashSet.add("ghi"));//true
        System.out.println(hashSet.add("def"));//false
        System.out.println(hashSet.size());//3

        System.out.println(hashSet);//[abc, def, ghi]
        HashSet<Animal> hashSet1 = new HashSet<>();
        hashSet1.add(new Animal("cat",10));
        hashSet1.add(new Animal("dog",5));
        hashSet1.add(new Animal("pig",1));
        hashSet1.add(new Animal("dog",5));
        for (Animal animal : hashSet1) {
            System.out.println(animal);
        }
    }
}
class Animal {
    private String name;
    private int age;

    public Animal() {}

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

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在这里插入图片描述

LinkedHashSet

package com.yuzhenc.collection;

import java.util.LinkedHashSet;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 21:16:17
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test13 {
    public static void main(String[] args) {
        //唯一、无序
        LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add(1);
        System.out.println(linkedHashSet.add(2));//true
        linkedHashSet.add(3);
        System.out.println(linkedHashSet.add(2));//false
        System.out.println(linkedHashSet);//[1, 2, 3]
    }
}

TreeSet

package com.yuzhenc.collection;

import java.util.TreeSet;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 21:20:31
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test14 {
    public static void main(String[] args) {
        //TreeSet底层利用的是内部比较器
        //逻辑结构:二叉树
        //物理结构:链式存储
        //特点:唯一、无序(不按添加的顺序输出)、有序(按升序输出)
        TreeSet<Integer> treeSet = new TreeSet<>();
        treeSet.add(45);
        treeSet.add(24);
        treeSet.add(38);
        treeSet.add(96);
        treeSet.add(11);
        treeSet.add(24);
        System.out.println(treeSet);//[11, 24, 38, 45, 96]
    }
}

内部比较器

package com.yuzhenc.collection;

import java.util.TreeSet;

/**
 * @author: yuzhenc
 * @date: 2022-03-09 21:27:41
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test15 {
    public static void main(String[] args) {
        TreeSet<Cat> treeSet = new TreeSet<>();
        treeSet.add(new Cat("Lili",18));
        treeSet.add(new Cat("Ana",19));
        treeSet.add(new Cat("Amy",20));
        treeSet.add(new Cat("Lili",18));
        treeSet.add(new Cat("Lili",19));
        treeSet.add(new Cat("Lili",17));
        for (Cat cat : treeSet) {
            System.out.println(cat);
        }
    }
}
class Cat implements Comparable <Cat>{
    String name;
    int age;

    public Cat() {}

    public Cat(String name, int age) {
        this.name = name;
        this.age = 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;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    @Override
    public int compareTo(Cat o) {
        return this.getAge()-o.getAge() == 0 ? this.getName().hashCode()-o.getName().hashCode() : this.getAge()-o.getAge();
    }
}

在这里插入图片描述

外部比较器

package com.yuzhenc.collection;

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

/**
 * @author: yuzhenc
 * @date: 2022-03-09 21:40:43
 * @desc: com.yuzhenc.collection
 * @version: 1.0
 */
public class Test16 {
    public static void main(String[] args) {
        Bijiao bijiao = new Bijiao();
        TreeSet<Dog> treeSet = new TreeSet<>(bijiao);
        treeSet.add(new Dog("Lili",18));
        treeSet.add(new Dog("Ana",19));
        treeSet.add(new Dog("Amy",20));
        treeSet.add(new Dog("Lili",18));
        treeSet.add(new Dog("Lili",19));
        treeSet.add(new Dog("Lili",17));
        for (Dog dog : treeSet) {
            System.out.println(dog);
        }
    }
}
class Dog {
    String name;
    int age;

    public Dog() {}

    public Dog(String name, int age) {
        this.name = name;
        this.age = 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;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Bijiao implements Comparator<Dog> {

    @Override
    public int compare(Dog o1, Dog o2) {
        return o1.getName().hashCode()-o2.getName().hashCode()==0 ? o1.getAge()-o2.getAge() : o1.getName().hashCode()-o2.getName().hashCode();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sqlboy-yuzhenc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值