4__List和Set集合(下)

来源:互联网 发布:固定电话测吉凶算法 编辑:程序博客网 时间:2024/06/04 19:14

Collection
|--List
|--Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复
|--HashSet:底层数据结构是哈希表
HashSet是如何保证数据唯一性?
是通过元素的两个方法,hashCode和equals来完成。
如果元素的hashCode值相同,才会判断equals是否为true.
如果元素的hashCode值不同,不会调用equals.

注意:对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法。

|--TreeSet:可以对Set集合中的元素进行排序。

底层数据结构是二叉树。

保证元素唯一性的依据:compareTo方法return 0。


TreeSet排序的第一种方式:让元素自身具备比较性。这就需要元素实现Comparable接口,覆盖CompareTo方法。


TreeSet的第二种方式。

当元素自身不及被比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。


Set集合的功能和Collection是一致的。




HashSet集合演示:

import java.util.HashSet;import java.util.Iterator;/*HashSet是如何保证数据唯一性?是通过元素的两个方法,hashCode和equals来完成。如果元素的hashCode值相同,才会判断equals是否为true.如果元素的hashCode值不同,不会调用equals.注意,对于判断元素是否存在,以及删除等操作,以来的方法是元素的hashCode方法和equals方法*/public class HashSetDemo {public static void main(String[] args) {// TODO Auto-generated method stubHashSet<Person> hs=new HashSet<Person>();hs.add(new Person("al",11));hs.add(new Person("a2",12));hs.add(new Person("a2",12));hs.add(new Person("a3",13));hs.add(new Person("a3",13));hs.add(new Person("a4",14));System.out.println(hs.contains(new Person("al",11)));Iterator<Person> it=hs.iterator();while(it.hasNext()){Person p=it.next();sop(p.getName()+"..."+p.getAge());}}public static void sop(Object obj){System.out.println(obj);}}class Person{private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object arg0) {// TODO Auto-generated method stubif(!(arg0 instanceof Person))return false;Person p=(Person)arg0;return this.name.equals(p.getName())&&this.age==p.getAge();}@Overridepublic int hashCode() {// TODO Auto-generated method stubreturn this.name.hashCode()+this.age*12;}}



TreeSet集合演示:

import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {// TODO Auto-generated method stub//要求:按照学生的年龄进行排序TreeSet<Student> ts=new TreeSet<Student>();//也可以传入自己的比较器//TreeSet<Student> ts1=new TreeSet<Student>(new MyCom());ts.add(new Student("lisi03",23));ts.add(new Student("lisi04",24));ts.add(new Student("lisi01",21));ts.add(new Student("lisi02",22));ts.add(new Student("lisi05",22));Iterator<Student> it=ts.iterator();while(it.hasNext()){Student stu=it.next();System.out.println(stu.getName()+"..."+stu.getAge());}}}class MyCom implements  Comparator{@Overridepublic int compare(Object arg0, Object arg1) {// TODO Auto-generated method stubif(!(arg0 instanceof Student)&&arg1 instanceof Student)throw new RuntimeException("不是Student类");Student stu0=(Student)arg0;Student stu1=(Student)arg1;int a=new Integer(stu0.getAge()).compareTo(new Integer(stu1.getAge()));//年龄相等时,我们在判断一下姓名是否相等if(a==0){return stu0.getName().compareTo(stu1.getName());}return a;}}class Student implements Comparable{private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic int compareTo(Object o) {// TODO Auto-generated method stubif(!(o instanceof Student))throw new RuntimeException("不是Student类");Student stu=(Student)o;int a=new Integer(this.age).compareTo(new Integer(stu.getAge()));//年龄相等时,我们在判断一下姓名是否相等if(a==0){return this.name.compareTo(stu.getName());}return a;}}





0 0
原创粉丝点击