一.Set
- Set的特点是无序、不可重复(唯一的),不可重复的底层原理是通过equals,来比较传入的元素的地址(因为Set存储的是对象的引用地址)所有其实现类都遵循改原则
- 注意:当添加的的String类型的元素时,因为String重新了equals,所有比较的是String里的具体内容
*/
public class Test {
public static void main(String[] args) {
Set hashSet=new HashSet();
String java1=new String("java");
String java2=new String("java");
hashSet.add(java1);
hashSet.add(java2);
System.out.println(hashSet.size());
}
}
1
二.HashSet
- HashSet是Set的具体实现类
- 增删等方法的使用
public class Test {
public static void main(String[] args) {
HashSet set=new HashSet();
set.add("a");
set.add("b");
set.add("c");
set.remove("a");
System.out.println(set.contains("a"));
System.out.println(set.isEmpty());
System.out.println(set.size());
}
}