HashSet集合和TreeSet集合
生活随笔
收集整理的這篇文章主要介紹了
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)題。
- 上一篇: Jeecg入门篇,高手掠过
- 下一篇: JEECG_3.7.2新版本入门讲解—U