java 容器 set_Java容器——Set接口

1.定义

set中不允许放入重复的元素(元素相同时只取一个)。它使用equals()方法进行比较,如果返回true,两个对象的HashCode值也应该相等。

2.方法

TreeSet中常用的方法:

boolean add(E e):添加一个元素,如果set中不存在该元素

boolean addAll(Collection extends E> c):向set添加集合

E ceiling(E e):返回大于等于给定元素的最小元素,没有返回null

void clear():移除所有元素

Object clone():浅拷贝集合

boolean contains(Object o):判断set是否包含某元素

E first():返回set的第一个元素

E last():返回set的最后一个元素

E floor(E e):返回给定元素的上一元素

E higher(E e):返回比给定元素大的最小元素

E lower(E e):返回比给定元素小的最大元素

SortedSet headSet(E toElement):返回不包含给定元素前面的所有元素

SortedSet tailSet(E fromElement):返回大于等于给定元素后面的所有元素

SortedSet subSet(E fromElement, E toElement):返回开始/结束元素之间的所有元素集合,[from, to)

NavigableSet headSet(E toElement, boolean inclusive):返回比给定元素小的元素集合,true表示小于等于

NavigableSet tailSet(E fromElement, boolean inclusive):返回比给定元素大的元素集合,true表示大于等于

boolean isEmpty():判断set是否为空

Iterator iterator():返回一个升序的set迭代器

E pollFirst():移除第一个元素,返回null如果set为空

E pollLast():移除最后一个元素,返回null如果set为空

boolean remove(Object o):移除指定元素

int size():返回集合元素数目

3.常用实现类

3.1 HashSet

1)可以放入空值;

2)传入元素时,调用HashCode方法获取hash值,然后决定存储位置;

3.2 LinkedHashSet

1)HashSet的子类,使用HashCode确定在集合中的位置,使用链表的方式确定位置(有序,按照输入的顺序输出)

3.3 TreeSet

1)默认情况下,直接使用TreeSet无参构造器创建Set的对象,在其中放入元素时,必须实现Comparable接口(用于排序),

按照compareTo方法排序;

2)若创建TreeSet对象时,传入了一个实现Comparator接口的类,则TreeSet使用Comparator接口的compare方法排序,

此时集合中的元素无需实现Comparable接口;如果放入了实现Comparable接口的元素,以Comparator为标准 。

4. 示例

4.1 SetFunc.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.util.*;2

3 public classSetFunc {4

5 public static voidmain(String[] args){6

7 //HashSet

8 Set c1 = new HashSet<>();9 c1.add(new Customer(1,"AAA"));10 c1.add(new Customer(1,"AAA"));11 c1.add(new Customer(2,"AAA"));12 c1.add(null);13 System.out.println(c1.size()); //3

14 for(Customer c:c1){15 System.out.println(c);16 }17

18

19 //LinkedHashSet

20 Set c2 = new LinkedHashSet<>();21 c2.add(new Customer(1,"AAA"));22 c2.add(new Customer(3,"CCC"));23 c2.add(new Customer(2,"BBB"));24 for(Customer c:c2){25 System.out.println(c);26 }27

28

29 /*

30 * TreeSet31 * 使用TreeSet()构造器32 * 需要为Customer类实现Comparable接口,即实现compareTo方法33 **/

34 TreeSet c3 = new TreeSet<>();35 c3.add(new Customer(1,"AAA"));36 c3.add(new Customer(3,"CCC"));37 c3.add(new Customer(4,"DDD"));38 c3.add(new Customer(5,"EEE"));39 Customer b = new Customer(2,"BBB");40 for(Customer c:c3){41 System.out.println(c);42 }43

44

45 //first

46 Customer a =c3.first();47 System.out.println("first: "+a); //Customer:[Id=1, Name=AAA]48 //last

49 Customer e =c3.last();50 System.out.println("last: "+e); //Customer:[Id=5, Name=EEE]51

52 //ceiling

53 Customer ceil1 =c3.ceiling(b);54 System.out.println("ceiling: : "+ceil1); //Customer:[Id=3, Name=CCC]55

56 //lower

57 a =c3.lower(b);58 System.out.println("lower b: "+a); //Customer:[Id=1, Name=AAA]

59 Customer pre =c3.lower(a);60 System.out.println("lower a: "+pre); //null61 //higher

62 Customer c =c3.higher(b);63 System.out.println("higher b: "+c); //Customer:[Id=3, Name=CCC]64

65 //floor

66 a =c3.floor(b);67 System.out.println("floor b: "+a); //Customer:[Id=1, Name=AAA]68

69 //subSet, [from, to)

70 Set c41 =c3.subSet(a, c);71 System.out.println("c41: "+c41);72

73 //headSet, [ , to)

74 Set c42 =c3.headSet(e);75 System.out.println("c42: "+c42);76 //headSet, [, to]

77 c42 = c3.headSet(e, true);78 System.out.println("c42: "+c42);79

80 //tailSet, [From, ]

81 Set c43 =c3.tailSet(c);82 System.out.println("c43: "+c43);83 //tailSet, (from, ]

84 c43 = c3.tailSet(c, false);85 System.out.println("c43: "+c43);86

87

88 /*

89 * TreeSet90 * 使用TreeSet(Comparator super E> comparator)构造器91 * 需要实现Comparator接口,即实现compare方法92 **/

93 Comparator comparator = newCustomerComparator();94 TreeSet c5 = new TreeSet<>(comparator);95 c5.add(new Customer(1,"AAA"));96 c5.add(new Customer(3,"CCC"));97 c5.add(new Customer(2,"BBB"));98 c5.add(new Customer(4,"DDD"));99 for(Customer cus:c5){100 System.out.println(cus);101 }102 }103 }

View Code

4.2 Customer.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.util.Objects;2

3 public class Customer implements Comparable{4

5 private intcustomerId;6 privateString customerName;7

8 publicCustomer(Integer customerId, String customerName) {9 this.customerId =customerId;10 this.customerName =customerName;11 }12

13 public intgetCustomerId() {14 returncustomerId;15 }16 publicString getCustomerName() {17 returncustomerName;18 }19

20 @Override21 publicString toString() {22 return "Customer:[Id=" + customerId + ", Name=" + customerName + "]";23 }24

25 /*

26 * 重写compareTo方法27 * 按Id或者name排序28 * 可以对整体添加负号决定升降序29 **/

30 @Override31 public intcompareTo(Customer o) {32 //return this.customerId - o.customerId;

33 return this.customerName.compareTo(o.customerName);34 }35

36 /*

37 * 重写equals和hashcode方法38 * 这里id和name相同则为同一对象39 **/

40 @Override41 public booleanequals(Object o) {42 if (this == o) return true;43 if (!(o instanceof Customer)) return false;44 Customer customer =(Customer) o;45 return customerId == customer.customerId &&

46 Objects.equals(customerName, customer.customerName);47 }48

49 @Override50 public inthashCode() {51 returnObjects.hash(customerId, customerName);52 }53

54 }

View Code

4.3 CustomerComparator.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.util.Comparator;2

3 public class CustomerComparator implements Comparator{4

5 @Override6 public intcompare(Customer c1, Customer c2) {7 //按Id排序

8 return c1.getCustomerId() -c2.getCustomerId();9 }10 }

View Code

!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值