一起Talk Android吧(第八十八回:Java中的類集之set二)

各位看官們,大家好,上一回中咱們說的是Java中類集之Set的例子,這一回咱們繼續說該例子。閒話休提,言歸正轉。讓我們一起Talk Android吧!

看官們,我們在前面章回中介紹了Set接口和它的實現類HashSet。本章回中將介紹Set接口的另外一個實現類TreeSetTreeSet表示有序的集合,而HashSet表示無序的集合,這是它們之間的不同點。這個順序不是指按照輸入數據時的順序,它是按照Comparable接口中compareTo()方法的結果來排序的。因此使用TreeSet存放自定義的數據類型時,需要實現該接口。存放內置數據類型時就不需要了。

TreeSet可以向HashSet一樣添加或者刪除數據,不過因爲TreeSet集合是有序集合,所以添加或者刪除數據會對集合排序,以保證整個集合的有序性。除此之外,它還有一些自己特有的方法:

  • first()和last()用來返回集合第一個和最後一個元素
  • headSet(obj)和tailSet(obj)用來返回在obj元素前面和後面所有元素組成的集合
  • subSet(from,to)返回 從from到to這個“區間”內元素組成的集合

這些操作涉及的大於或者小於某個數據元素,大於或者小於的比較是基於equals()和hashCode()方法,而且大於操作包含元素本身,可以看作是大於等於某個數據,小於操作則不包含元素本身(可以通過程序運行結果看出來)。

準確來講TreeSetSortedSet接口的實現類,而SortedSet接口又是Set接口的子接口,因此可以把TreeSet類看作是Set接口的實現類。不過在代碼中最好把TreeSet類對象轉換爲SortedSet接口對象,而不是Set接口對象,不然好多方法無法使用。下面是具體的代碼,請參考:

import java.util.Arrays;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class setEx {
    public static void main(String args[]){
        // init set
        SortedSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < 10; ++i) {
            // set.add(new Integer(i+1));
            set.add((i + 1));
        }

        // show size of set ,and content of set
        System.out.println("size of set: " + set.size());
        for (Integer i : set)
            System.out.print(i + " ");

        System.out.println("\nset: " + set);

        // delete the content of set,this is based on content of set
        if(set.remove(9)) {
            System.out.println("after removing the content 9. set: " + set);
        } else {
            System.out.println("removing the content 9 failed.");
        }

        // delete the same content of set,it is failed to remove
        if(set.remove(9)) {
            System.out.println("after removing the content 9. set: " + set);
        } else {
            System.out.println("removing the content 9 failed.");
        }

        // add the same content of set,it is failed to add 
        if(set.add(9)) {
            System.out.println("after adding the content 9. set: " + set);
        } else {
            System.out.println("adding the content 9 failed.");
        }

        // add the content of set,this is based on content of set
        if(set.add(9)) {
            System.out.println("after adding the content 9. set: " + set);
        } else {
            System.out.println("adding the content 9 failed.");
        }

        // change set to array
        Integer[] array = set.toArray(new Integer[] {});
        System.out.println("change set to array: " + Arrays.toString(array));

        //------------ this is for TreeSet ---------------
        System.out.println("TreeSet operation: first: "+set.first() );
        System.out.println("TreeSet operation: last: "+set.last() );
        System.out.println("TreeSet operation: headSet 6: "+set.headSet(6) );
        System.out.println("TreeSet operation: TailSet 6: "+set.tailSet(6) );
        System.out.println("TreeSet operation: subSet[3,9]: "+set.subSet(3,9));

        //------------ this is for TreeSet ---------------
        // add the content of set,this is based on content of set
        set.clear();
        System.out.println("after clearing the set: " + set);
    }
}

下面是程序的運行結果,請參考:

size of set: 10
1 2 3 4 5 6 7 8 9 10 
set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
after removing the content 9. set: [1, 2, 3, 4, 5, 6, 7, 8, 10]
removing the content 9 failed.
after adding the content 9. set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
adding the content 9 failed.
change set to array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
TreeSet operation: first: 1
TreeSet operation: last: 10
TreeSet operation: headSet 6: [1, 2, 3, 4, 5]
TreeSet operation: TailSet 6: [6, 7, 8, 9, 10]
TreeSet operation: subSet[3,9]: [3, 4, 5, 6, 7, 8]
after clearing the set: []

各位看官,關於Java中類集之Set的例子咱們就介紹到這裏,欲知後面還有什麼例子,且聽下回分解!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章