HashSet的使用

HashSet的使用
package com.etc.chapater0_1;

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

/**
 *HashSet集合的使用
 * 存储结构:哈希表(数组+链表+红黑树)
 *
 */

public class Demo02 {
    public static void main(String[] args) {
        //新建一个集合
        HashSet<String> hashSet = new HashSet<String>();
        //1.添加元素
        hashSet.add("林志玲");
        hashSet.add("周润发");
        hashSet.add("梁朝伟");
        hashSet.add("刘德华");
        System.out.println(hashSet.size());
        System.out.println(hashSet.toString());
        //2.删除数据
//        hashSet.remove("");
//        System.out.println(hashSet.size());
        //3.遍历
        //使用增强for
        System.out.println("---------使用增强for-----------");
        for (String s:hashSet
             ) {
            System.out.println(s.toString());
        }
        //使用迭代器
        System.out.println("----------使用迭代器--------------");
        Iterator<String> iterator = hashSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //判断
        System.out.println(hashSet.contains("刘德华"));
        System.out.println(hashSet.isEmpty());
    }
}

HashSet使用(2)

package com.etc.chapater0_1;

public class Person {
    private String name;
    private int age;
    public Person() {
    }

    public Person(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 "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int hashCode() {
        int n1 = this.name.hashCode();
        int n2 = this.age;
        return n1+n2;
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj){
            return true;
        }
        if (obj==null){
            return true;
        }
        if (obj instanceof Person){
            Person p = (Person) obj;
            if (this.name.equals(p.getName())&&this.age==p.getAge()){
                return true;
            }
        }
        return false;
    }
}

package com.etc.chapater0_1;

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

/**
 *HashSet的使用
 * 储存结构:哈希表(数组+链表+红黑树)
 * 储存过程:
 * 1.根据HashCode计算保存的位置  如果此位置为空 则直接保存 如果不为空则执行第二步
 * 2.在执行equals方法  如果equals方法为true 则认为是重复 否则 形成链表
 */

public class Demo03 {
    public static void main(String[] args) {
        //创建集合
        HashSet<Person> hashSet = new HashSet<Person>();
        //添加元素
        Person p1 = new Person("liudehua",22);
        Person p2 = new Person("linzhilin",21);
        Person p3 = new Person("liaochaowei",25);
        Person p4 = new Person("lixiaolong",23);
        hashSet.add(p1);
        hashSet.add(p2);
        hashSet.add(p3);
        hashSet.add(p4);
        hashSet.add(new Person("liudehua",22));
        System.out.println(hashSet.size());
        System.out.println(hashSet.toString());

        //2.删除操作
//        hashSet.remove(p1);
//        hashSet.remove(new Person("liudehua",22));
//        System.out.println(hashSet.size());
        //遍历
        //使用增强for
        System.out.println("---------使用增强for-----------");
        for (Person p:hashSet
             ) {
            System.out.println(p.toString());
        }

        //使用迭代器
        System.out.println("--------------使用迭代器-------------");
        Iterator<Person> iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //判断
        System.out.println(hashSet.isEmpty());
        System.out.println(hashSet.contains(new Person("liudehua",22)));

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值