Set接口实现类-HashSet
HashSet底层机制说明
HashSet底层是HashMap,HashMap底层是(数组+链表+红黑树)
@SuppressWarnings({"all"})
public class HashSetStructure {
public static void main(String[] args) {
//1.模拟一个HashSet的底层(HashMap的底层结构)
//2.创建一个数组,数组类型是Node[]
Node[] table = new Node[16];
System.out.println("table = " + table);
//3.创建结点
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;
Node lucy = new Node("Lucy", null);
table[3] = lucy;//把Lucy放到table表的索引为3的位置
System.out.println("table = " + table);
}
}
class Node{
//结点,存储数据,可以指向下一个结点,从而形成链表
Object item;//存放数据
Node next;//指向下一个结点
public Node(Object item, Node next) {
this.item = item;
this.next = next;
}
}