日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HashSet集合和TreeSet集合

發(fā)布時(shí)間:2025/3/16 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HashSet集合和TreeSet集合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
-----------------------------------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);}}}///從寫(xiě)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的作用是獲得對(duì)象的hash值//重寫(xiě)hashCodepublic int hashCode(){return id.hashCode();}//重寫(xiě)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);}}}/重寫(xiě)TreeSet集合的compareTo方法 package ji_he;class People implements Comparable{ //實(shí)現(xiàn)Comparable接口String name;int age;public People(String name,int age){this.name=name;this.age=age;}public String toString (){ //重寫(xiě)toString方法return name+":"+age; }@Override public int compareTo(Object o) { //重寫(xiě)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;//自定義一個(gè)比較器 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());}}}

總結(jié)

以上是生活随笔為你收集整理的HashSet集合和TreeSet集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。