java基础——集合类Collection——Set
/*集合类Collection[注意:泛型]
* 子类 List Set(都是接口interface)
*
* List实现类 ArrayList LikedList Stack Vector
* Set 实现类 HashSet TreeSet
*
* 学习推荐 :集Set、列表List、映射 Map
* */
1 package collection;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 public class collectionTest {
8 public static void main(String[] args) {
9 /*
10 * Set元素无序保存,元素不可重复
11 */
12 Set<Object> set = new HashSet<Object>();
13 set.add(1);
14 set.add(1);
15 set.add(3);
16 Object str1 = "集合类Set元素1";
17 Object str2 = "集合类Set元素2";
18 Object str3 = "集合类Set元素3";
19 set.add(str1);
20 set.add(str2);
21 set.add(str3);
22 /* for (Object obj : set) {
23 System.out.println(obj);
24 }*/
25 Iterator<Object> ite = set.iterator();
26 while(ite.hasNext()){
27 Object o = ite.next();
28 System.out.println(o);
29 }
30 }
31
32 }
上述代码中遍历方式有两种:
直接遍历:
for (Object obj : set) {System.out.println(obj);}
实现了Iterable接口的类型遍历:
Iterator<Object> ite = set.iterator();
while(ite.hasNext()){Object o = ite.next();System.out.println(o);}
效果一致!!!
Set还有以下方法可以使用

/*************************************************************************************************/
以下百度复制【由于在项目中没有接触TreeSet用法】
/*
*基于 TreeMap 的 NavigableSet 实现。使用元素的自然顺序对元素进行排序,
*或者根据创建 set 时提供的 Comparator 进行排序,具体取决于使用的构造方法
*
*排序:
*自然排序
*比较器排序
*(何种排序取决于new TreeSet的构造方法!)
*
* 注意:为什么是会排序的呢?
* 因为底层是用了comparable的compareto方法!
*/
在java中 Set集合中的元素是唯一的,那么Set元素的唯一是根据什么判断?
根据 对象元素覆盖实现的hashCode和equals方法,
因此在用对象做Set的元素的时候,需要覆盖并重写这两个方法,避免出现意外的问题。
排序的Set集合,自然排序是根据equals来排序的,
如果使用 实现comparetor 或者 comparable 类来进行排序 ,
其中的实现方法compareTo的比较逻辑需要和equals相同,否则会出现少元素的问题。
java中compareable/comparetor/compareTo的区别
推荐查阅:http://www.cnblogs.com/tp123/p/6401994.html
ComparcompareTo 是 Comparable 接口定义的方法。
Comparable 表示内在的顺序,如整数(大小),字符串(字典序)
Comparator 表示的是某一特性的顺序。如按人口/面积/GDP之类对国家排序
1 package collection;
2
3 import java.util.Set;
4 import java.util.TreeSet;
5
6 public class TreeSetTest {
7 public static void main(String[] args) {
8 Set<Student> ts = new TreeSet<Student>();
9 Student s1 = new Student("one", 1);
10 Student s2 = new Student("two", 2);
11 Student s3 = new Student("three", 3);
12 Student s4 = new Student("four", 4);
13 Student s5 = new Student("five", 5);
14 Student s6 = new Student("six", 6);
15 // 注意下面重复项
16 Student s7 = new Student("one", 1);
17 Student s8 = new Student("six", 6);
18 ts.add(s1);
19 ts.add(s2);
20 ts.add(s3);
21 ts.add(s4);
22 ts.add(s5);
23 ts.add(s6);
24 ts.add(s7);
25 ts.add(s8);
26 for (Student s : ts) {
27 System.out.println(s.getAge() + s.getName());
28 }
29 }
30 }
31 /*注意:
32 * 因为底层是用了comparable的comparetor方法!所以学生类要实现comparable并重写
33 */
34 class Student implements Comparable<Student> {
35 private String name;
36 private int age;
37
38 public Student(String name, int age) {
39 this.name = name;
40 this.age = age;
41 }
42
43 public int compareTo(Student o) {
44 // 按年龄进行排序:
45 int num = this.age - o.age;
46 // 去掉重复!
47 if (num == 0) {
48 this.name.compareTo(o.name);
49 }
50 return num;
51 }
52
53 public String getName() {
54 return name;
55 }
56
57 public void setName(String name) {
58 this.name = name;
59 }
60
61 public int getAge() {
62 return age;
63 }
64
65 public void setAge(int age) {
66 this.age = age;
67 }
68
69 }
上述代码亮点
public int compareTo(Student o) {
// 按年龄进行排序:
int num = this.age - o.age;
// 去掉重复!
if (num == 0) {
this.name.compareTo(o.name);
}
return num;
}
创建一个线程安全的set集合
可以利用Collections工具类的synchronizedSet方法修饰一个非同步的Set变成同步Set
例如:
Set set = new HashSet(); // 非同步,非线程安全的Set
Set syncSet = Collections.synchronizedSet(set); // 返回了一个线程安全的Set
posted on 2017-03-16 00:06 weizhang715 阅读(118) 评论(0) 收藏 举报
浙公网安备 33010602011771号