1 package com.common.api; 2 3 import java.util.HashSet; 4 import java.util.Iterator; 5 6 /* 7 * Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复 8 * |---HashSet:底层数据结构是哈希表 9 * HashSet是如何保证元素唯一性的? 10 * 1)通过元素的两个方法,hashCode何equals来完成 11 * 2)如果元素的hashcode相同,才会判断equals是否为true 12 * 3)如果元素的hashCode值不同,不会调用equals 13 * 注意:对于判断元素是否存在以及删除等操作,以来的方法是元素的hashcode和equals 14 * |---TreeSet: 15 * Set 集合的功能和Collection是一致的 16 * boolean add(E e) 如果指定的元素不存在,则将其指定的元素添加(可选操作)。 17 * boolean addAll(Collection<? extends E> c) 将指定集合中的所有元素添加到此集合(如果尚未存在)(可选操作)。 18 * void clear() 从此集合中删除所有元素(可选操作)。 19 * boolean contains(Object o) 如果此集合包含指定的元素,则返回 true 。 20 * boolean containsAll(Collection<?> c) 返回 true如果此集合包含所有指定集合的元素。 21 * boolean equals(Object o) 将指定的对象与此集合进行比较以实现相等。 22 * boolean isEmpty() 如果此集合不包含元素,则返回 true 。 23 * Iterator<E> iterator() 返回此集合中元素的迭代器。 24 * boolean remove(Object o) 如果存在,则从该集合中删除指定的元素(可选操作)。 25 * boolean removeAll(Collection<?> c) 从此集合中删除指定集合中包含的所有元素(可选操作)。 26 * boolean retainAll(Collection<?> c) 仅保留该集合中包含在指定集合中的元素(可选操作)。 27 * int size() 返回此集合中的元素数(其基数)。 28 * Object[] toArray() 返回一个包含此集合中所有元素的数组。 29 * 30 * HashSet 常用方法 31 * boolean add(E e) 如果指定的元素不存在,则将其指定的元素添加(可选操作)。 32 * boolean addAll(Collection<? extends E> c) 将指定集合中的所有元素添加到此集合(如果尚未存在)(可选操作)。 33 * void clear() 从此集合中删除所有元素(可选操作)。 34 * boolean contains(Object o) 如果此集合包含指定的元素,则返回 true 。 35 * boolean containsAll(Collection<?> c) 返回 true如果此集合包含所有指定集合的元素。 36 * boolean equals(Object o) 将指定的对象与此集合进行比较以实现相等。 37 * boolean isEmpty() 如果此集合不包含元素,则返回 true 。 38 * Iterator<E> iterator() 返回此集合中元素的迭代器。 39 * boolean remove(Object o) 如果存在,则从该集合中删除指定的元素(可选操作)。 40 * boolean removeAll(Collection<?> c) 从此集合中删除指定集合中包含的所有元素(可选操作)。 41 * boolean retainAll(Collection<?> c) 仅保留该集合中包含在指定集合中的元素(可选操作)。 42 * int size() 返回此集合中的元素数(其基数)。 43 * Object[] toArray() 返回一个包含此集合中所有元素的数组。 44 */ 45 46 class Person 47 { 48 private String name; 49 private int age; 50 Person(String name,int age) 51 { 52 this.name=name; 53 this.age=age; 54 } 55 public String getName() { 56 return name; 57 } 58 public void setName(String name) { 59 this.name = name; 60 } 61 public int getAge() { 62 return age; 63 } 64 public void setAge(int age) { 65 this.age = age; 66 } 67 68 public boolean equals(Object obj) 69 { 70 if(!(obj instanceof Person)) 71 { 72 return false; 73 } 74 Person p=(Person) obj; 75 System.out.println(this.name+"......"+p.name); 76 return this.name.equals(p.name) && this.age==p.age; 77 } 78 public int hashCode() 79 { 80 return name.hashCode()+age*3; 81 } 82 } 83 public class HashSetDemo { 84 85 public static void sop(Object obj) 86 { 87 System.out.println(obj); 88 } 89 public static void method_base() { 90 HashSet hs=new HashSet(); 91 hs.add("java01"); 92 hs.add("java02"); 93 hs.add("java03"); 94 hs.add("java04"); 95 sop(hs.add("java01")); 96 97 Iterator it=hs.iterator(); 98 while(it.hasNext()) 99 { 100 sop(it.next()); 101 } 102 } 103 104 public static void method_custom() 105 { 106 //往hashSet集合中存入自定义对象 107 //姓名和年龄相同为同一个人,重复元素 108 109 HashSet hs=new HashSet(); 110 hs.add(new Person("a1",11)); 111 hs.add(new Person("a2",12)); 112 hs.add(new Person("a3",13)); 113 hs.add(new Person("a4",14)); 114 hs.add(new Person("a1",11)); 115 116 Iterator it=hs.iterator(); 117 while(it.hasNext()) 118 { 119 Person p=(Person) it.next(); 120 sop(p.getName()+" : "+p.getAge()); 121 } 122 123 sop(hs.contains(new Person("a1",11))); 124 hs.remove(new Person("a1",11)); 125 System.out.println("after remove ..."); 126 127 Iterator it2=hs.iterator(); 128 while(it2.hasNext()) 129 { 130 Person p=(Person) it2.next(); 131 sop(p.getName()+" : "+p.getAge()); 132 } 133 } 134 public static void main(String[] args) { 135 // TODO Auto-generated method stub 136 method_base(); 137 138 method_custom(); 139 } 140 }
执行结果如下:
false java04 java03 java02 java01 a1......a1 a1 : 11 a2 : 12 a3 : 13 a4 : 14 a1......a1 true a1......a1 after remove ... a2 : 12 a3 : 13 a4 : 14