Collection接口之子接口Set
Set接口的实现类:hashSet,linkedHashSet,TreeSet;
Set接口使用的比较少,了解即可
1 /** 2 * Collection接口之Set子接口 3 * set接口:无序的不可重复的数据 4 * HashSet:作为set接口的主要实现类,线程不安全效率高,可以存储null值 5 * LinkedHashSet:作为HashSet的子类,遍历内部数据时,可以按照存储的顺序遍历 6 * TreeSet:可以按照添加元素的指定属性进行排序 7 * ******************** 8 * 要求:向Set中添加数据一定要重写其数据所在类的equals与hashCode方法 9 ********************** 10 * 无序性:不代表随机性,存储的数据在底层数组中并非按照索引的顺序排列,是按照hashcode值进行排序 11 * 不可重复性:保证添加的元素按照equals方法判断时,不能返回true,即相同的元素只可以有一个 12 *二:添加元素的过程:以hashset为例: 13 * 我们向hashset中添加元素a,首先调用a所在类的hashCode方法,计算出a的哈希值 14 * 此哈希值再通过某种算法计算出再HashSet底层数组中存放的位置,判断数组此位置上是否已经有元素 15 * 如果此位置无元素,则a添加成功,如果有元素b,则比较a与b的哈希值,如果哈希值不相同则a添加成功 16 * 如果哈希值相同则需要调用元素a所在类的equals方法,如果方法返回true,a添加失败,否则添加成功 17 * 18 * 19 * 20 */ 21 public class SetTest { 22 23 @Test 24 public void test() { 25 HashSet set=new HashSet(); 26 set.add(123); 27 set.add(456); 28 set.add("hpp"); 29 set.add("hpp"); 30 set.add(LocalDateTime.now()); 31 System.out.println(set); 32 } 33 @Test 34 public void test2(){ 35 Set set=new LinkedHashSet(); 36 set.add(123); 37 set.add("hpp"); 38 set.add(426); 39 System.out.println(set); 40 } 41 @Test 42 public void test3(){ 43 /** 44 * treeSet不能添加不同类的对象:只能是相同类的对象 45 * 两种排序方式:自然排序与定制排序,用的Comparable接口与Comparator接口 46 * 47 */ 48 TreeSet set=new TreeSet(); 49 //set.add("hpp"); 50 set.add(new Student(5,"zh")); 51 set.add(new Student(15,"si")); 52 set.add(new Student(25,"huang")); 53 set.add(new Student(6,"tang")); 54 set.add(new Student(6,"li")); 55 System.out.println(set); 56 for (Object o:set){ 57 System.out.println(o); 58 } 59 60 } 61 @Test 62 public void test4(){ 63 //按照年龄从大到小排序,年龄相同按照姓名排序 64 Comparator comparator=new Comparator() { 65 @Override 66 public int compare(Object o1, Object o2) { 67 if (o1 instanceof Student&& o2 instanceof Student) { 68 Student student1 = (Student) o1; 69 Student student2 = (Student) o2; 70 int a = Integer.compare(student1.getAge(), student2.getAge()); 71 if (a == 0) { 72 return student1.getName().compareTo(student2.getName()); 73 } else { 74 return Integer.compare(student1.getAge(), student2.getAge()); 75 } 76 } 77 throw new RuntimeException("类型错误"); 78 } 79 }; 80 TreeSet set=new TreeSet(comparator);//后面如果为空参代表使用自然排序,使用形参为定制排序 81 set.add(new Student(5,"zh")); 82 set.add(new Student(15,"si")); 83 set.add(new Student(25,"huang")); 84 set.add(new Student(6,"tang")); 85 set.add(new Student(6,"li")); 86 System.out.println(set); 87 for (Object o:set){ 88 System.out.println(o); 89 } 90 } 91 92 93 } 94 class Student implements Comparable{ 95 private int age; 96 private String name; 97 98 @Override 99 public String toString() { 100 return this.age+this.name; 101 } 102 103 public int getAge() { 104 return age; 105 } 106 107 public void setAge(int age) { 108 this.age = age; 109 } 110 111 public String getName() { 112 return name; 113 } 114 115 public void setName(String name) { 116 this.name = name; 117 } 118 119 public Student(int age, String name) { 120 this.age = age; 121 this.name = name; 122 } 123 //按照姓名从小到大排列 124 // @Override 125 // public int compareTo(Object o) { 126 // if(o instanceof Student){ 127 // Student student=(Student)o; 128 // return -this.getName().compareTo(student.getName()); 129 // }else { 130 // throw new RuntimeException("类型不匹配"); 131 // } 132 // } 133 /** 134 * 135 ********************************** 136 */ 137 //按照年龄排序:必须对于基本数据类型必须判断大小,返回一个整数值 138 @Override 139 public int compareTo(Object o) { 140 if(o instanceof Student){ 141 Student student=(Student)o; 142 if (this.age>student.age){ 143 return 1; 144 }else if (this.age<student.age){ 145 return -1; 146 }else { 147 return -this.name.compareTo(student.name); 148 } 149 } 150 throw new RuntimeException("数据类型异常"); 151 } 152 }
浙公网安备 33010602011771号