JAVA 集合(2) Set

摘要:JAVA 集合(2) Set

HashSet:


class Hello{
    public static void main(String[] args){
        HashSet hs = new HashSet();
        hs.add("a");
        hs.add("b");
        hs.add("c");
        hs.add("d");
        hs.add("b");
        hs.add("d");
        sop(hs);//[d, b, c, a]  不可重覆 且無序
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

在HashSet內存入自定義物件,檢查有無重覆元素


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 int getAge(){
        return age;
    }
    //需先判斷hashcode是否一樣才會調用equals方法
    //如此才會去除重覆元素
    public int hashCode(){
        return name.hashCode() + age;//使用String類的hashCode()方法
    }
    public boolean equals(Object obj){
        if(!(obj instanceof Person)){
            return false;
        }else{
            Person p = (Person)obj;
            return this.name.equals(p.name) && this.age == p.age;
        }
    }
}
class Hello{
    public static void main(String[] args){
        HashSet hs = new HashSet();
        hs.add(new Person("a", 20));
        hs.add(new Person("b", 21));
        hs.add(new Person("c", 22));
        hs.add(new Person("d", 23));
        hs.add(new Person("c", 22));
       
        Iterator it = hs.iterator();
        while(it.hasNext()){
            Person p = (Person)it.next();
            sop(p.getName() +": " + p.getAge());
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

TreeSet:


class Hello{
    public static void main(String[] args){
        TreeSet ts = new TreeSet();
        ts.add("d");
        ts.add("c");
        ts.add("b");
        ts.add("a");
        sop(ts); //[a, b, c, d]
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

TreeSet應用自定義類別,加入元素,並以年齡來排序,但是當主要條件相同時,一定要比較次要條件,排序方法有兩種:
  • 實現Comparable介面,覆寫compareTo方法
  • 實現Comparator介面,覆寫compare方法

 


//該介面強制讓Student具備比較性, 否則會發生異常
//因為其不具比較性
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 int getAge(){
        return age;
    }
    public int compareTo(Object obj){
        if(!(obj instanceof Student)){
            throw new RuntimeException("不屬於此類");
        }
        Student s = (Student)obj;
        if(this.age > s.age){
            return 1;
        }else if(this.age == s.age){
            return this.name.compareTo(s.name);
        }else{
            return -1;
        }
    }
}

class Hello{
    public static void main(String[] args){
        TreeSet ts = new TreeSet();
        ts.add(new Student("a", 25));
        ts.add(new Student("b", 23));
        ts.add(new Student("c", 21));
        ts.add(new Student("d", 22));
       
        Iterator it = ts.iterator();
        while(it.hasNext()){
            Student s = (Student)it.next();
            //依年齡排序
            sop(s.getName() + ": " + s.getAge());// c: 21, d: 22, b: 23, a: 25
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

 

二元樹排序 依名稱排序:

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 int getAge(){
        return age;
    }
    public int compareTo(Object obj){
        if(!(obj instanceof Student)){
            throw new RuntimeException("不屬於此類");
        }
        Student s = (Student)obj;
        if(this.age > s.age){
            return 1;
        }else if(this.age == s.age){
            return this.name.compareTo(s.name);
        }else{
            return -1;
        }
    }
 
}
 
public class Main{
    public static void main(String[] args){
        TreeSet ts = new TreeSet(new MyCompare());
        ts.add(new Student("a", 25));
        ts.add(new Student("b", 23));
        ts.add(new Student("c", 21));
        ts.add(new Student("d", 22));
        ts.add(new Student("a", 15));
        ts.add(new Student("d", 40));
       
        Iterator it = ts.iterator();
        while(it.hasNext()){
            Student s = (Student)it.next();
            sop(s.getName() + ": " + s.getAge());
            //a: 15, a: 25, b: 23, c: 21, d: 22, d: 40
            //依name排序,name相等在依age排序
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}
 
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){
            /**if(s1.getAge() >s2.getAge()){
                return 1;
            }
            if(s1.getAge() == s2.getAge()){
                return 0;
            }
            return -1;*/
            //使用Integer類的compareTo方法
            return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
        }
        return num;
    }
}
 
 
字串本身具備比較性,但是他的方法不是所需要的,所以需要使用比較器,先比較長度,若長度相等再比較字串大小

public class Main{
    public static void main(String[] args){
        TreeSet ts = new TreeSet(new StringLengthCompare());
        ts.add("ajij");
        ts.add("bc");
        ts.add("cb");
        ts.add("cweef");
        ts.add("dwwsw");
       
        Iterator it = ts.iterator();
        while(it.hasNext()){
            sop(it.next());
        }
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
}

class StringLengthCompare implements Comparator{
    public int compare(Object o1, Object o2){
        String s1 = (String)o1;
        String s2 = (String)o2;
       
        int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
        //長度相等比次要元素
        if(num == 0){
            return s1.compareTo(s2);
        }
        return num;
    }
}