Set集合

Set接口:存储无序的,不可重复的数据 ---->高中讲的“集合”
Set接口是Collection的子接口,set接口没有提供额外的方法。但是比Collection接口更加严格了。

Set 集合不允许包含相同的元素,如果试把两个相同的元素加入同一个 Set 集合中,则添加操作失败。

Set集合支持的遍历方式和Collection集合一样:foreach和Iterator。

Set的常用实现类有:HashSet、TreeSet、LinkedHashSet。

 * HashSet: 作为Set接口的主要实现类 线程是不安全的 可以存储null值* LinkedHashSet:作为HashSet子类 遍历其内部数据是  可以按照添加的顺序遍历* TreeSet:可以按照添加对象的指定属性 进行排序* *  Set   存储无序的 不可重复的数据*  无序性:不等于随机性  存储的数据在底层数组中并非按照数组索引的顺序添加 而是根据数据的哈希值添加的*  不可重复性: 加了重复相同的元素也没办法被获取到  保证添加的元素按照equals()判断是 不能返回true 相同元素只能添加一个*  HashSet底层是以数组加链表的形式存在  通过特定的哈希值决定储存位置的*  向Set中添加的数据 其所在的类一定要重写hashCode()和equals()方法

代码测试:

    @Testpublic void test(){Set set = new HashSet();set.add(456);set.add(1232);set.add("AA");set.add("BB");set.add(new Person("tom",20));set.add(new Person("tom",20));set.add(1232);Iterator iterator = set.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}

测试结果:

以及LinkedHashSet的测试:

     //LinkedHashSet@Testpublic void test2(){Set set = new LinkedHashSet();set.add(456);set.add(1232);set.add("AA");set.add("BB");set.add(new Person("tom",20));set.add(new Person("tom",20));set.add(1232);Iterator iterator = set.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}

测试结果如下:

TreeSet

底层结构:里面维护了一个TreeMap,都是基于红黑树实现的!

特点:
1、不允许重复
2、实现排序
自然排序或定制排序 --------> 不能用equals()
(如果使用的是自然排序,则通过调用实现的compareTo方法
如果使用的是定制排序,则通过调用比较器的compare方法)

  • 1
    @Testpublic void test1(){TreeSet treeSet = new TreeSet();treeSet.add(123);treeSet.add(32);treeSet.add(43);Iterator iterator = treeSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}

如果加的不是相同类的话无法通过运行 可以通过编译
运行结果:

自然顺序

如果试图把一个对象添加到 TreeSet 时,则该对象的类必须实现 Comparable 接口。实现 Comparable 的类必须实现 compareTo(Object obj) 方法,两个对象即通过 compareTo(Object obj) 方法的返回值来比较大小。对于 TreeSet 集合而言,它判断两个对象是否相等的唯一标准是:两个对象通过 compareTo(Object obj) 方法比较返回值为0。

自然排序代码格式:

    @Overridepublic int compareTo(java.lang.Object o) {if (o instanceof Person){Person p = (Person) o;
//            return this.name.compareTo(p.name);if (this.name.equals(p.name)){//                return Integer.compare(this.age,p.age);if (this.age>p.age){return 1;}else if (this.age<p.age){return -1;}else if (this.age==p.age){return 0;}}elsereturn this.name.compareTo(p.name);}throw new RuntimeException("数据异常错误");}

使用TreeSet测试:

    @Testpublic void test2(){TreeSet treeSet = new TreeSet();treeSet.add(new Person("Jack",20));treeSet.add(new Person("Peter",32));treeSet.add(new Person("Peter",12));treeSet.add(new Person("Tom",12));treeSet.add(new Person("Tom",12));treeSet.add(new Person("Buff",43));treeSet.add(new Person("Buff",21));Iterator iterator = treeSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}

运行结果如下:

定制排序

如果放到TreeSet中的元素的自然排序(Comparable)规则不符合当前排序需求时,或者元素的类型没有实现Comparable接口。那么在创建TreeSet时,可以单独指定一个Comparator的对象。使用定制排序判断两个元素相等的标准是:通过Comparator比较两个元素返回了0。

示范如比较各人的生日的年月日来比较的话从大到小:

  • 1 先写好两个类:
public class Employee implements Comparable {private String name;private int age;private MyDate birthday;public Employee() {}public Employee(MyDate birthday) {this.birthday = birthday;}public Employee(String name, int age, MyDate birthday) {this.name = name;this.age = age;this.birthday = birthday;}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;}public MyDate getBirthday() {return birthday;}public void setBirthday(MyDate birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", age=" + age +", birthday=" + birthday +'}';}@Overridepublic int compareTo(Object o) {if ( o instanceof  Employee){Employee e = (Employee) o;return this.name.compareTo(e.name);}throw new RuntimeException("数据异常");}
}public class MyDate {private int year;private int month;private int day;public MyDate() {}public MyDate(int year, int month, int day) {this.year = year;this.month = month;this.day = day;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}public int getMonth() {return month;}public void setMonth(int month) {this.month = month;}public int getDay() {return day;}public void setDay(int day) {this.day = day;}@Overridepublic String toString() {return "MyDate{" +"year=" + year +", month=" + month +", day=" + day +'}';}
}
  • 2 使用定制方法:
    @Testpublic void test1(){TreeSet set = new TreeSet(new Comparator() {@Overridepublic int compare(Object o1, Object o2) {if (o1 instanceof Employee && o2 instanceof Employee){Employee e1 = (Employee) o1;Employee e2 = (Employee) o2;MyDate m1 = e1.getBirthday();MyDate m2 = e2.getBirthday();int minusYear = m1.getYear() - m2.getYear();if (minusYear != 0){if (minusYear > 0){return 1;}else return -1;}int minusMonth = m1.getMonth()-m2.getMonth();if (minusMonth != 0){if (minusMonth >0){return 1;}else return -1;}int minusDay = m1.getDay()-m2.getDay();if (minusDay != 0){if (minusDay>0){return 1;}else  return -1;}return 0;}throw new RuntimeException("数据异常");}});Employee employee = new Employee("Tom", 21, new MyDate(2002, 9, 26));Employee employee1 = new Employee("Jack",54,new MyDate(1996,2,8));Employee employee2 = new Employee("Ross",12,new MyDate(1988,4,3));Employee employee3 = new Employee("Peter",68,new MyDate(2004,9,12));Employee employee4 = new Employee("Tonny",34,new MyDate(2012,11,21));Employee employee5 = new Employee("XXX",34,new MyDate(1988,11,21));Employee employee6 = new Employee("YYYY",34,new MyDate(2002,9,21));Employee employee7 = new Employee("MMM",34,new MyDate(2002,9,21));set.add(employee);set.add(employee1);set.add(employee2);set.add(employee3);set.add(employee4);set.add(employee5);set.add(employee6);set.add(employee7);Iterator iterator = set.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}

运行结果如下:

Set集合的基本使用相关推荐

  1. do还是doing imagine加to_中学必背英语短语集合:54个doing动名词的固定搭配

    中学必背英语短语集合:54个doing动名词的固定搭配​mp.weixin.qq.com doing动名词是中小学英语教学中的重要内容.在小学的时候老师大概会把doing解释为一般进行时,但层级越往上 ...

  2. Redis 笔记(07)— sorted set 类型(添加、删除有序集合元素、获取分数范围内成员、按score排序、返回集合元素个数)

    zset 可能是 Redis 提供的最为特色的数据结构,一方面它是一个 set,保证了内部 value 的唯一性,另一方面它可以给每个 value 赋予一个 score,代表这个 value 的排序权 ...

  3. Redis 笔记(06)— set 类型(向集合添加元素、获取集合元素个数、判断集合中是否包含某个元素、删除给定元素、返回集合中所有元素、计算集合的交集、并集、差集)

    Redis 的 set 集合内部的键值对是无序的唯一的.它的内部实现相当于一个特殊的字典,字典中所有的 value 都是一个值 NULL .当集合中最后一个元素移除之后,数据结构自动删除,内存被回收. ...

  4. 【C#】集合_哈希表_字典_泛型_文件

    数组能做到:存放同种类型数据,且数据个数确定 object类型的数组能满足:放各种类型的数据,确定放多少个,但是随意插入元素,数组做不到 集合能做到:存放各种数据类型,且不确定存放多少个,能做到随意插 ...

  5. java集合中对象某属性比较排序

    TreeSet:它可以给Set集合中的元素进行指定方式的排序. 保证元素唯一性的方式:通过比较的结果是否为0. 底层数据结构是:二叉树. 排序的第一种方式: 让元素自身具备比较性.只要让元素实现Com ...

  6. 程序员应该吃透的集合List

    一:先看看集合框架接口图 (图片来源于网络) 从图中可以看到List实现了Collection接口. 二:Collection接口是什么? 在java类库中,Collection接口是集合类的基本接口 ...

  7. Java集合详解之Map

    一.首先看看集合框架体系图 从图中可以看到,Map接口扩展了Iterator接口,关于Iterator接口详解请移步:Iterator接口详解 二.Map是什么? Map<k,v>使用键值 ...

  8. 第一个python程序:定义,列表,元组,集合,求并集交集,键和值,运算符,缩进

    ''' 来源:天善智能韦玮老师课堂笔记 ''' print("定义") a = 6 # python里无需定义 print("a=",a) a += 1 # + ...

  9. python 获取集合里面的某一个元素

    python 获取集合里面的某一个元素,想想呢集合是不支持所以,切片,相加,相乘操作的, 所以想获取集合里面的某一个元素需要转化下思路,比如把即可转成列表然后在利用索引获取 例如: list_a = ...

  10. python 把列表或者元组转成集合

    python 把列表或者元组转成集合 使用set 方法 list_a = ["张三", "李四", "王二"] # 把列表转成集合 prin ...

最新文章

  1. error C1004 发现意外的文件尾
  2. python3 通过列表作为键 产生字典
  3. linux查找文件或字符串的命令
  4. MVC架构设计——EF-Code First
  5. 鼠标移动到ul图片会摆动_我们可以从摆动时序分析中学到的三件事
  6. 2017 Google 开发者大会直播入口
  7. 汇编语言mul指令_跟着开源软件学汇编语言:计算器
  8. 国外的电子商务开发情况
  9. sql数据库批量插入
  10. java垃圾收集方法_java几种垃圾收集方法和垃圾收集器
  11. opencv2.4.9中HoughlinesP源码中的疑问解析!
  12. system.out输出到文件上
  13. 判断最小生成树的唯一性
  14. 微信公众号开发相关流程及功能介绍
  15. 模拟电路-模拟加法器的设计和仿真
  16. 嵌入式设备的switch 以及PHY 芯片调试和选型 (2)
  17. windows 10 电脑必备软件
  18. 骁龙780g相当于什么处理器 骁龙780g什么水平
  19. 【String】字符串中是否有空格(indexOf、split)
  20. Win10蓝屏原因查找以及解决

热门文章

  1. DirectShow编程
  2. [信号基础] 信号频率,采样率,采样点(快拍数)等
  3. 索引的底层实现原理是什么?
  4. STM32 外部中断详解(原理+配置代码)
  5. C语言:extern用法
  6. Python爬虫|Python爬虫入门:请求
  7. aws 邮件服务器 接收邮件,Amazon SES
  8. Python字符串对齐方法(ljust()、rjust()和center())详解
  9. 普通文本el-tootip超出宽度自动显示省略号,悬停显示
  10. spring boot check/token Principal 如何注入