凌风博客原创作品。转载请注明出处:http://blog.youkuaiyun.com/q549130180/article/details/45313445
|--Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复,线程是非同步的
|--HashSet:底层数据结构式哈希表HashSet是如何保证元素唯一性的呢?
是通过元素的两个方法,hashCode和equals来完成
如果元素的HashCode值相同,才会判断equals是否为true
如果元素的Hashcode值不同,才会调用equals
注意:对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法
|--TreeSet:可以对Set集合中的元素进行排序
底层数据结构式二叉树
保证元素唯一性的依据
compareTo方法return 0
TreeSet排序的第一种方式:让元素自身具备比较性
元素需要实现Comparable接口,覆盖compareTo方法
这种方式也称为元素的自然顺序,或者叫做默认顺序
TreeSet的第二种排序方式
当元素自身不具备比较性时,或者具备的比较性不是所需要的
这时就需要让集合自身具备比较性
在集合初始化时,就有了比较方式
Set集合的功能和Collection是一致的。
- class HashSetDemo1
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args)
- {
- HashSet hs = new HashSet();
- hs.add(new Person("a1",11));
- hs.add(new Person("a2",12));
- hs.add(new Person("a3",13));
- hs.add(new Person("a2",12));
- sop("a1:"+hs.contains(new Person("a2",12)));
- //hs.remove(new Person("a3",13));
- Iterator it = new iterator();
- while (it.hasNext)
- {
- Person p = (Person)it.next();
- sop(p.getName()+"::"+p.getAge());
- }
- }
- }
- class Person
- {
- Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int hashCode()
- {
- System.out.println(this.name+"....hashCode");
- return name.hashCode()+age;
- }
- public boolean equals(Object obj)
- {
- if (!(obj instanceof Person))
- {
- return false;
- }
- System.out.println(this.name+"..."+p.name);
- return this.name.equals(p.name) && this.age ==p.age;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
需求:
往TreeSet集合中存储自定义对象学生
想按照学生的年龄进行排序
记住:排序时,当主要条件相同时,一定判断一下次要条件
- class TreeSetDemo2
- {
- public static void main(String[] args)
- {
- TreeSet ts = new TreeSet();
- ts.add(new Studet("lisi02",22));
- ts.add(new Studet("lisi07",20));
- ts.add(new Studet("lisi09",19));
- ts.add(new Studet("lisi08",19));
- ts.add(new Studet("lisi07",20));
- Iterator it = ts.iterator();
- while (it.hasNext())
- {
- Studet stu = (Student)it.next();
- System.out.println(stu.getName()+"..."+stu.get.Age());
- }
- }
- }
- class Student implements Comparable //该接口强制让学生具备比较性
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int compareTo(Object obj)
- {
- if (!(obj instanceof Student))
- {
- throw new RuntimeException("不是学生对象");
- }
- Student s = (Student)obj;
- System.out.println(this.name+"..compareto.."+s.name);
- if(this.age>s.age)
- return 1;
- if(this.age==s.age)
- return this.name.compareTo(s.name);
- return -1;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }
当元素自身不具备比较性,或者具备的比较性不是所需要的
这时需要让容器自身具备比较性。
定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数
当两种排序都存在时,以比较器为主
定义一个类,实现Comparator接口,覆盖compare方法
- class TreeSetDemo2_1
- {
- public static void main(String[] args)
- {
- TreeSet ts = new TreeSet(new MyCompare());
- ts.add(new Studet("lisi02",22));
- ts.add(new Studet("lisi07",20));
- ts.add(new Studet("lisi09",19));
- ts.add(new Studet("lisi08",19));
- ts.add(new Studet("lisi07",20));
- Iterator it = ts.iterator();
- while (it.hasNext())
- {
- Studet stu = (Student)it.next();
- System.out.println(stu.getName()+"..."+stu.get.Age());
- }
- }
- }
- class MyCompare implements Comparator
- {
- public int compare(Object o1,Object o2)
- {
- Student s1 = (Student)o1;
- Student s2 = (Student)o2;
- int num = s1.getName().compareTo(s2.getName());
- if (num==0)
- {
- return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
- /*
- if (s1.getAge()>s2.getAge)
- return 1;
- if (s1.getAge()==s2.getAge())
- return 0;
- return -1;
- */
- }
- return num;
- }
- }
- class Student implements Comparable //该接口强制让学生具备比较性
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int compareTo(Object obj)
- {
- if (!(obj instanceof Student))
- {
- throw new RuntimeException("不是学生对象");
- }
- Student s = (Student)obj;
- System.out.println(this.name+"..compareto.."+s.name);
- if(this.age>s.age)
- return 1;
- if(this.age==s.age)
- return this.name.compareTo(s.name);
- return -1;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- }