HashSet集合和TreeSet集合

来源:互联网 发布:矩阵乘以矩阵的转置 编辑:程序博客网 时间:2024/05/23 12:05
-----------------------------------HashSet集合///////////HashSet集合的遍历package ji_he;import java.util.HashSet;import java.util.Iterator;public class Example09 {public static void main(String[] args) {// TODO Auto-generated method stubHashSet set=new HashSet();set.add("Jack");set.add("Eve");set.add("Rose");set.add("Rose");Iterator iterator=set.iterator();while (iterator.hasNext()) {Object object = (Object) iterator.next();System.out.println(object);}}}///////////从写hashCode方法和equals方法package ji_he;public class Student { private String id;private String name;public Student(String id, String name) {       this.id=id;      this.name=name;}public String toString() {return id+":"+name;}//hashCode的作用是获得对象的hash值//重写hashCodepublic int hashCode(){return id.hashCode();}//重写equals方法public boolean equals(Object obj){if (this==obj) {return true;}if (!(obj instanceof Student)) {return false;}Student stu=(Student) obj;boolean b=this.id.equals(stu.id);return b;}}package ji_he;import java.util.*;public class Examp10 {public static void main(String[] args) {// TODO Auto-generated method stubHashSet hs=new HashSet();Student stu1=new Student("1","Jack");Student stu2=new Student("2","Rose");Student stu3=new Student("2","Rose");hs.add(stu1);hs.add(stu2);hs.add(stu3);System.out.println(hs);}}--------------------------------------------------------TreeSet集合///////////////TreeSet集合的遍历package ji_he;import java.util.*;public class Example12 {public static void main(String[] args) {// TODO Auto-generated method stubTreeSet treeSet=new TreeSet();treeSet.add("Jack");treeSet.add("Helean");treeSet.add("Eve");Iterator it=treeSet.iterator();while (it.hasNext()) {Object object =  it.next();System.out.println(object);}}}/////////////重写TreeSet集合的compareTo方法package ji_he; class People implements Comparable{  //实现Comparable接口 String name; int age; public People(String name,int age){ this.name=name; this.age=age; } public String toString (){   //重写toString方法return name+":"+age;}@Overridepublic int compareTo(Object o) {   //重写compareTo方法// TODO Auto-generated method stubPeople s=(People) o;if (this.age-s.age>0) {return 1;}if (this.age-s.age==0) {return this.name.compareTo(s.name);}return -1;} }package ji_he;import java.util.Iterator;import java.util.TreeSet;public class Example13 {public static void main(String[] args) {// TODO Auto-generated method stub// TODO Auto-generated method stubTreeSet treeSet=new TreeSet();treeSet.add(new People("Jack", 19));treeSet.add(new People("Helean", 18));treeSet.add(new People("Rose", 17));treeSet.add(new People("Tom", 16));treeSet.add(new People("Rose", 17));Iterator it=treeSet.iterator();while (it.hasNext()) {Object object =  it.next();System.out.println(object);}}}////////////////自定义比较器import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;//自定义一个比较器class Mycompare implements Comparator{@Overridepublic int compare(Object o1, Object o2) {// TODO Auto-generated method stubMinStudent ms1=(MinStudent )o1;MinStudent ms2=(MinStudent )o2;int i=ms1.getName().compareTo(ms2.getName());if(i==0)return ms1.getAge()-ms2.getAge();return i;}}public class MyCompareDemos {public static void main(String[] args) {// TODO Auto-generated method stubTreeSet ts = new TreeSet(new Mycompare());ts.add(new MinStudent("ccc",22));ts.add(new MinStudent("ddd",22));ts.add(new MinStudent("aaa",21));ts.add(new MinStudent("dad",23));ts.add(new MinStudent("fff",25));ts.add(new MinStudent("sss",22));ts.add(new MinStudent("jjj",20));Iterator it = ts.iterator();while(it.hasNext()){MinStudent ms = (MinStudent)it.next();System.out.println(ms.toString());}}}