Set容器特点

①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序;

②   最常用的两个Set接口的实现类是HashSet和TreeSet;

HashSet及常用API

①   HashSet扩展AbstractSet并且实现Set接口;

②   HashSet使用散列表(又称哈希表)进行存储;

③   构造方法:

a)   HashSet()

b)   HashSet(Collection c)

c)   HashSet(int capacity)

d)   HashSet(int capacity,float fillRatio)

④   HashSet没有定义任何超过它的父类和接口提供的其它方法;

⑤   散列集合没有确保其元素的顺序,因为散列处理通常不参与排序;

         HashSet<String> data=new HashSet<String>();
data.add("张三");
data.add("李四");
data.add("jay");
data.add("jack");
data.add("jay");
System.out.println(data);

输出结果:

[李四, 张三, jay, jack]

此处第二个jay没有存入;

可以将其打印出来System.out.println(data.add("jay"));,结果显示第一个为true,第二个为false

编写一个Student类:

 class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
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;
}
}

主方法中添加及输出

         HashSet<Student> stuSet=new HashSet<Student>();
System.out.println(stuSet.add(new Student("张三",20)));
System.out.println(stuSet.add(new Student("李四",30)));
System.out.println(stuSet.add(new Student("张三",20)));
System.out.println(stuSet.size());

输出结果:

true

true

true

3

由此可见:new Student("张三",20)两次都创建了,若想字相同时只创建一次则需重构hashCode和equals方法

如下:

     @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

再次执行,输出结果:

true

true

false

2

总结:HashSet的内部操作的底层数据是HashMap,只是我们操作的是HashMap的key;

Set容器——HashSet及常用API的更多相关文章

  1. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  2. List容器——LinkedList及常用API,实现栈和队列

    LinkedList及常用API ①   LinkedList----链表 ②   LinkedList类扩展AbstractSequentialList并实现List接口 ③   LinkedLis ...

  3. List容器——ArrayList及常用API

    List: ①   List容器是有序的collection(也称为序列).此接口的用户可以对List容器中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜 ...

  4. Set容器——TreeSet及常用API

    TreeSet及常用Api ①   TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快; ②   在存储了大量的需要进行快速检索的排序信息的情况下,TreeSe ...

  5. Map容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用

    Map接口 ①   映射(map)是一个存储键/值对的对象.给定一个键,可以查询到它的值,键和值都是对象; ②   键必须是唯一的,值可以重复; ③   有些映射可以接收null键和null值,而有的 ...

  6. Java | 个人总结的Java常用API手册汇总

    目录 常用API JavaAPI 1 java.lang String StringBuilder Integer parseXxx Math Object System Throwable Thre ...

  7. java基础3.0:Java常用API

    本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...

  8. JQuery常用API 核心 效果 JQueryHTML 遍历 Event事件

    JQuery 常用API 参考资料:JQuery 官网   jQuery API 中文文档 核心 jQuery 对象 jQuery() 返回匹配的元素集合,无论是通过在DOM的基础上传递的参数还是创建 ...

  9. 常用API接口汇总

    下面列举了100多个国内常用API接口,并按照 笔记.出行.词典.电商.地图.电影.即时通讯.开发者网站.快递查询.旅游.社交.视频.天气.团队协作.图片与图像处理.外卖.消息推送.音乐.云.语义识别 ...

随机推荐

  1. Windows平台下如何在C#中调用Python

    最近迷上了Python,发现它能够做很多C#无法完成的事情,比如,调用CMD或者在CMD中执行一个exe文件命令行并获得输出的结果.过程简单,处理起来也非常方便,但如果要用C#调用Python文件呢, ...

  2. 2017.10.1 QBXT 模拟赛

    题目链接 T1 枚举右端点,前缀和优化.对于当前点x,答案为 sum[x][r]-sum[x][l-1]-(sum[z][r]-sum[z][l-1]) 整理为 sum[x][r]-sum[z][r] ...

  3. self & this 上下文

    对象:指向对象的首地址: 函数:代表了函数运行的主要上下文: 内部:在类的内部使用. self Within the body of a class method, self refers to th ...

  4. 查看numpy的类型

    查看一个变量的类型:type(img) 查看array中的数据值的类型:img.dtype 查看array的形状:img.shape

  5. java 自定义容器,实现foreach

    import java.util.Arrays; import java.util.Iterator; public class ArrayList implements Iterable<In ...

  6. Java获取yml里面的配置

    #yml文件配置systemPath: #档案系统地址 dossier: http://127.0.0.1:8088/ //调用说明 配置文件里必须包含节点 否则项目无法启动 @Value(" ...

  7. 记录一次mysql中自定义获取UUID的函数

    循环方式一: DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() ) begin ) ...

  8. c++作业:递归调用,例题4.5 求第五个人的年龄

    递归调用,例题4.5 求第五个人的年龄 #include <iostream> using namespace std; int age(int num){ int a; ) a=; el ...

  9. ES6学习总结 (二)

    一:ES6为函数做了哪些扩展 参数的默认值 传统写法: function person(n,a){ var name =n || "zhangsan"; var age = a | ...

  10. 【Machine Learning is Fun!】1.The world’s easiest introduction to Machine Learning

    Bigger update: The content of this article is now available as a full-length video course that walks ...