java中set介面的用法,javaset介面用法

來源:互聯網
上載者:User

java中set介面的用法,javaset介面用法

java中的set介面有如下的特點:

  • 不允許出現重複元素;
  • 集合中的元素位置無順序;
  • 有且只有一個值為null的元素。

因為java中的set介面模仿了數學上的set抽象,所以,對應的數學上set的特性為:

  • 互異性:一個集合中,任何兩個元素都認為是不相同的,即每個元素只能出現一次。
  • 無序性:一個集合中,每個元素的地位都是相同的,元素之間是無序的。集合上可以定義序關係,定義了序關係後,元素之間就可以按照序關係排序。但就集合本身的特性而言,元素之間沒有必然的序。
  • 空集的性質:空集是一切集合的子集

 

      Set不儲存重複的元素。Set中最常被使用的是測試歸屬性,你可以很容易的詢問某個對象是否在某個Set中。Set具有與Collection完全一樣的介面,因此沒有任何額外的功能。實際上Set就是Collection,只是行為不同。

  實現了Set介面的主要有HashSet、TreeSet、LinkedHashSet這幾個共同點就是每個相同的項只儲存一份。他們也有不同點,區別如下:

  1.HashSet:

  HashSet使用的是相當複雜的方式來儲存元素的,使用HashSet能夠最快的擷取集合中的元素,效率非常高(以空間換時間)。會根據hashcode和equals來龐端是否是同一個對象,如果hashcode一樣,並且equals返回true,則是同一個對象,不能重複存放。

package cn.set;import java.util.HashSet;import java.util.Set;class Student{    int id;    public Student(int id) {        this.id = id;    }    @Override    public String toString() {        return this.id+"";    }    @Override    public int hashCode() {        return this.id;    }    @Override    public boolean equals(Object obj) {        if (obj instanceof Student){            Student  stu = (Student) obj;            if (stu.id == this.id)                return true;        }        return false;    }}public class HashSetTest {    public static void main(String[] args) {        Set<Student> set = new HashSet<Student>();        Student s1 = new Student(1);        Student s2 = new Student(1);        Student s3 = new Student(2);        set.add(s1);        set.add(s2);        set.add(s3);        for (Student s : set) {            System.out.println(s);        }    }}

正如上例所示,重寫了hashCode()和equals()方法來區分同意對象後,就不能存放同以對象了。如果注釋這兩個方法,則所有Student對象視為不同對象,都可以存放。

 

 2.TreeSet

  TreeSet也不能存放重複對象,但是TreeSet會自動排序,如果存放的對象不能排序則會報錯,所以存放的對象必須指定定序。定序包括自然排序和客戶排序。

  ①自然排序:TreeSet要添加哪個對象就在哪個對象類上面實現java.lang.Comparable介面,並且重寫comparaTo()方法,返回0則表示是同一個對象,否則為不同對象。

  ②客戶排序:建立一個第三方類並實現java.util.Comparator介面。並重寫方法。定義集合形式為TreeSet ts = new TreeSet(new 第三方類());

下面一個例子用TreeSet存放自然排序的對象:

package cn.set;import java.util.Set;import java.util.TreeSet;class Student1 implements Comparable<Student1>{    int id;    public Student1(int id) {        this.id = id;    }    @Override    public String toString() {        return this.id+"";    }    @Override    public int hashCode() {        return this.id;    }    @Override    public boolean equals(Object obj) {        if (obj instanceof Student1){            Student1  stu = (Student1) obj;            if (stu.id == this.id)                return true;        }        return false;    }    public int compareTo(Student1 o) {        return (this.id-o.id);    }}public class TreeSetTest {    public static void main(String[] args) {        Set<Student1> set = new TreeSet<Student1>();        Student1 s1 = new Student1(5);        Student1 s2 = new Student1(1);        Student1 s3 = new Student1(2);        Student1 s4 = new Student1(4);        Student1 s5 = new Student1(3);        set.add(s1);        set.add(s2);        set.add(s3);        set.add(s4);        set.add(s5);        for (Student1 s : set) {            System.out.println(s);        }    }}

輸出結果為: 1 2 3 4 5

下面一個例子用TreeSet存放客戶排序的對象:

package com.set;import java.util.Set;import java.util.TreeSet;class Student1 implements Comparable<Student1>{    int id;    public Student1(int id) {        this.id = id;    }    @Override    public String toString() {        return this.id+"";    }    @Override    public int hashCode() {        return this.id;    }    @Override    public boolean equals(Object obj) {        if (obj instanceof Student1){            Student1  stu = (Student1) obj;            if (stu.id == this.id)                return true;        }        return false;    }    public int compareTo(Student1 o) {        return (this.id-o.id);    }}public class TreeSetTest {    public static void main(String[] args) {        Set<Student1> set = new TreeSet<Student1>();        Student1 s1 = new Student1(5);        Student1 s2 = new Student1(1);        Student1 s3 = new Student1(2);        Student1 s4 = new Student1(4);        Student1 s5 = new Student1(3);        set.add(s1);        set.add(s2);        set.add(s3);        set.add(s4);        set.add(s5);        for (Student1 s : set) {            System.out.println(s);        }    }}

輸出結果為: 5 4 3 2 1

大家都知道List存放時按照插入順序排序的,其實也可以用自然排序和客戶排序對List集合排序,大家請看:

package cn.set;import java.util.ArrayList;import java.util.Collections;import java.util.List;class MySort1 implements java.util.Comparator<Student3>{    public int compare(Student3 o1, Student3 o2) {        return o2.id-o1.id;    }}class Student3 implements Comparable<Student3>{    int id;    public Student3(int id) {        this.id = id;    }    @Override    public String toString() {        return this.id+"";    }    public int compareTo(Student3 o) {        return (this.id-o.id);    }}public class ListSort {    public static void main(String[] args) {        List<Student3> list = new ArrayList<Student3>();        Student3 s1 = new Student3(5);        Student3 s2 = new Student3(1);        Student3 s3 = new Student3(2);        Student3 s4 = new Student3(4);        Student3 s5 = new Student3(3);        list.add(s1);        list.add(s2);        list.add(s3);        list.add(s4);        list.add(s5);        System.out.println(list);        //自然排序:        Collections.sort(list);        System.out.println(list);        //客戶排序        Collections.sort(list, new MySort1());        System.out.println(list);    }}

輸出結果為:

[5, 1, 2, 4, 3]

[1, 2, 3, 4, 5]

[5, 4, 3, 2, 1]

轉載來自:http://www.cnblogs.com/liuling/archive/2013/04/16/set.html有助於學學習

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.