Java学习第四十二天<Set接口方法><HashSet说明><HashSet链表><HashSet链表断点><HashSet扩容机制><HashSet练习>

​

Set接口方法

​

 

package chapter17.Set;
​
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
​
public class SetMethod {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add("john");
        set.add("lucy");
        set.add("john");
        set.add(null);
        System.out.println(set);//[null, john, lucy] set接口的实现类的对象(Set接口对象),不能存放重复元素,可添加null,存放数据无序(按哈希值排序)
​
        //遍历
        Iterator iterator = set.iterator();//用迭代器
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }
        System.out.println("==================");
        for (Object o:set){
            System.out.println(o);//增强for
        }
        System.out.println("==================");
        //set没有get方法,不能用普通for的索引遍历
        set.remove(null);
        System.out.println(set);
    }
}

 

HashSet说明

package chapter17.Set;
​
import java.util.HashSet;
import java.util.Set;
​
public class HashSet01 {
    public static void main(String[] args) {
      Set hashSet = new HashSet();//底层是HashMap 看源码
        hashSet.add(null);
        hashSet.add(null);
        System.out.println(hashSet);//HashSet 只能存一个null
          //添加成功,返回true
        System.out.println(hashSet.add("xx"));//T
        System.out.println(hashSet.add("yy"));//T
        System.out.println(hashSet.add("xx"));//F
        System.out.println(hashSet.remove("xy"));//F
        System.out.println(hashSet);//[null, xx, yy]
​
​
    }
}

 

package chapter17.Set;
​
import java.util.HashSet;
import java.util.Set;
​
public class HashSet02 {
    public static void main(String[] args) {
        Set set = new HashSet();
        System.out.println(set);
        set.add("lucy");
        set.add("lucy");//地址相同,加不成功
        set.add(new Dog("tom"));
        set.add(new Dog("tom"));//地址不同,可加成功
        System.out.println(set);
        set.add(new String("yy"));
        set.add(new String("yy"));//不同堆,但指向一样,加不了
        System.out.println(set);//[yy, Dog{name='tom'}, Dog{name='tom'}, lucy]
    }
}
class Dog{
    private String name;
​
    public Dog(String name) {
        this.name = name;
    }
​
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }
}

 

HashSet链表

package chapter17.Set;
​
public class HashSetStructure {
    public static void main(String[] args) {
        //模拟一个HashSet底层(HashMap底层结构)
        //1.创建一个数组 类型 Node[] 可称为表
        Node[] table = new Node[16];
​
        Node john = new Node("john", null);
        table[2]=john;
        Node jack = new Node("jack", null);
        john.next=jack; //将jack节点挂载到john
        Node rose = new Node("Rose", null);
        jack.next=rose;//将rose节点挂载到jack
        System.out.println(table);
​
    }
}
class Node{//节点,存储数据,可指向下一节点,形成链表
    Object item;//存数据
    Node next;//指向下一节点
​
    public Node(Object item, Node next) {
        this.item = item;
        this.next = next;
    }
}

 

HashSet链表断点

package chapter17.Set;
​
import java.util.HashSet;
​
public class HashSetSource {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add("java");
        hashSet.add("php");
        hashSet.add("java");
        System.out.println(hashSet);
    }
}

​

 

HashSet扩容机制

package chapter17.Set;
​
import java.util.HashSet;
​
public class HashSetIncrement {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        for (int i = 1; i <=100 ; i++) {
            hashSet.add(i);//1-100
        }
​
        HashSet hashSet1 = new HashSet();
        for (int i = 1; i <=12 ; i++) {
            hashSet1.add(new A(i));
        }
        System.out.println(hashSet1);
                //链数变多达到条件后也会扩容(即使节点处没被占满)
    }
}
class A{
    private int n;
​
    public A(int n) {
        this.n = n;
    }
​
    @Override
    public int hashCode() {
        return 100;//哈希值一样,索引一样,能让所有元素放一个链上
    }
}

​

 

HashSet练习

package chapter17.Set;
​
import java.util.HashSet;
import java.util.Objects;
​
public class HashSetExercise {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Employee("xx",6));
        hashSet.add(new Employee("yy",8));
        hashSet.add(new Employee("xx",6));
        System.out.println(hashSet);
    }
}
class Employee{
    private String name;
    private int age;
​
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
//重写equals和hashCode,使得姓名和年龄一样时,返回相同hashCode值和equals,全一样就不添加
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }
​
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
​
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

package chapter17.Set;
​
import java.util.HashSet;
import java.util.Objects;
​
public class HashSetExercise02 {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Employee2("xx",58.8, 1993,2,25));//有
        hashSet.add(new Employee2("xx",58.8, 1993,2,25));//无
        hashSet.add(new Employee2("xx",60, 1993,2,25));//无
        hashSet.add(new Employee2("xx",59.8, 1994,2,25));//有
        hashSet.add(new Employee2("yy",5, 1993,2,25));//有
        System.out.println(hashSet);
​
    }
}
class Employee2{
    private String name;
    private double sal;
    MyDate birthday;//在大类中可调用
    public  Employee2(String name, double sal,int year,int month,int day) {
        this.name = name;
        this.sal = sal;
        birthday = new MyDate(year, month, day);
    }
​
    static class MyDate{
        private int year;
        private int month;
        private int day;
​
         public int getYear() {
             return year;
         }
​
         public int getMonth() {
             return month;
         }
​
         public int getDay() {
             return day;
         }
​
         public MyDate(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
​
        @Override//year, month, day属性相同时,则认为是同一个birthday
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            MyDate myDate = (MyDate) o;
            return year == myDate.year && month == myDate.month && day == myDate.day;
        }
​
        @Override
        public int hashCode() {
            return Objects.hash(year, month, day);
        }
    }
​
    @Override//name, birthday 相同时,认为是同一员工
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee2 employee2 = (Employee2) o;
        return Objects.equals(name, employee2.name) && Objects.equals(birthday, employee2.birthday);
    }
​
    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }
​
    @Override
    public String toString() {
        return "Employee2{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                '}';
    }
}
​
posted @ 2022-04-27 09:46  征途**  阅读(75)  评论(0)    收藏  举报