/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author huanghuankun
*/
public class HashSetTest {
public static void main(String[] args) {
HashSet<Number> set = new HashSet<Number>();
byte num_byte = 1;
int num_int = 1;
Integer num_int1 = 1;
long num_long = 1;
set.add(num_byte);
set.add(num_int);
set.add(num_int1);//包装类和其对应的基本类型,在set中视为重复,不同类型hashcode不同
set.add(num_long);
System.out.println("set.sieze()=" + set.size());
}
}
输出:
set.sieze()=3
本文深入探讨了Java集合框架中的HashSet类的使用及特性,包括HashSet的大小计算、元素添加逻辑、基本类型与包装类的处理方式,并通过实例展示了HashSet的实际应用。
172万+

被折叠的 条评论
为什么被折叠?



