Set接口实现类TreeSet源码及详解

一.TreeSet简介

 

此类实现Set接口,由二叉树实现的,TreeSet中的数据是自动排好序的,不允许放入null值。

 

 

 

 

二.TreeSet源码

 

package java.util;

import java.io.Serializable;

public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable {
    public TreeSet() {
        throw new RuntimeException("Stub!");
    }

    public TreeSet(Comparator comparator) {
        throw new RuntimeException("Stub!");
    }

    public TreeSet(Collection c) {
        throw new RuntimeException("Stub!");
    }

    public TreeSet(SortedSet s) {
        throw new RuntimeException("Stub!");
    }

    public Iterator iterator() {
        throw new RuntimeException("Stub!");
    }

    public Iterator descendingIterator() {
        throw new RuntimeException("Stub!");
    }

    public NavigableSet descendingSet() {
        throw new RuntimeException("Stub!");
    }

    public int size() {
        throw new RuntimeException("Stub!");
    }

    public boolean isEmpty() {
        throw new RuntimeException("Stub!");
    }

    public boolean contains(Object o) {
        throw new RuntimeException("Stub!");
    }

    public boolean add(E e) {
        throw new RuntimeException("Stub!");
    }

    public boolean remove(Object o) {
        throw new RuntimeException("Stub!");
    }

    public void clear() {
        throw new RuntimeException("Stub!");
    }

    public boolean addAll(Collection c) {
        throw new RuntimeException("Stub!");
    }

    public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
        throw new RuntimeException("Stub!");
    }

    public NavigableSet headSet(E toElement, boolean inclusive) {
        throw new RuntimeException("Stub!");
    }

    public NavigableSet tailSet(E fromElement, boolean inclusive) {
        throw new RuntimeException("Stub!");
    }

    public SortedSet subSet(E fromElement, E toElement) {
        throw new RuntimeException("Stub!");
    }

    public SortedSet headSet(E toElement) {
        throw new RuntimeException("Stub!");
    }

    public SortedSet tailSet(E fromElement) {
        throw new RuntimeException("Stub!");
    }

    public Comparator comparator() {
        throw new RuntimeException("Stub!");
    }

    public E first() {
        throw new RuntimeException("Stub!");
    }

    public E last() {
        throw new RuntimeException("Stub!");
    }

    public E lower(E e) {
        throw new RuntimeException("Stub!");
    }

    public E floor(E e) {
        throw new RuntimeException("Stub!");
    }

    public E ceiling(E e) {
        throw new RuntimeException("Stub!");
    }

    public E higher(E e) {
        throw new RuntimeException("Stub!");
    }

    public E pollFirst() {
        throw new RuntimeException("Stub!");
    }

    public E pollLast() {
        throw new RuntimeException("Stub!");
    }

    public Object clone() {
        throw new RuntimeException("Stub!");
    }

    public Spliterator spliterator() {
        throw new RuntimeException("Stub!");
    }
}

 

 

 

 

 

 

三.TreeSet常用方法

 

<1> size():获取集合的长度

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
int num=treeSet.size();

 

 

<2> isEmpty():集合是否为空

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
boolean b=treeSet.isEmpty();

 

 

<3> add(E e):向集合中添加元素

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
treeSet.add("aaaa");

 

 

<4> contains(Object o):集合中是否包含o元素

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
treeSet.add("aaaa");
boolean b=treeSet.contains("ffff");
boolean c=treeSet.contains("FFFFFF");
Log.d("TAG","b----:"+b);
Log.d("TAG","c----:"+c);
b----:true

c----:false

 

 

<5> remove(Object o):删除o元素

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
treeSet.add("aaaa");
treeSet.add("aaaa");
treeSet.add("bbbb");
treeSet.add("bbbb");
treeSet.add("cccc");
treeSet.add("cccc");
treeSet.add("dddd");
int num1=treeSet.size();
Log.d("TAG","num1----:"+num1);
treeSet.remove("dddd");
int num2=treeSet.size();
Log.d("TAG","num2----:"+num2);
num1----:5

num2----:4

 

 

<6> clear():清空集合

TreeSet treeSet=new TreeSet();
treeSet.add("ffff");
treeSet.add("aaaa");
treeSet.add("aaaa");
treeSet.add("bbbb");
treeSet.add("bbbb");
treeSet.add("cccc");
treeSet.add("cccc");
treeSet.add("dddd");
int num1=treeSet.size();
Log.d("TAG","num1----:"+num1);
treeSet.clear();
int num2=treeSet.size();
Log.d("TAG","num2----:"+num2);
num1----:5

num2----:0

 

 

<7> iterator():遍历集合

package com.example.mytest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.util.Iterator;
import java.util.TreeSet;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSet();
    }

    private void initSet() {
        TreeSet treeSet = new TreeSet();
        treeSet.add("ffff");
        treeSet.add("aaaa");
        treeSet.add("aaaa");
        treeSet.add("bbbb");
        treeSet.add("bbbb");
        treeSet.add("cccc");
        treeSet.add("cccc");
        treeSet.add("dddd");

        Iterator i = treeSet.iterator();
        while (i.hasNext()) {
            Log.d("TAG", i.next() + "");
        }
    }

}
D/TAG: aaaa

D/TAG: bbbb

D/TAG: cccc

D/TAG: dddd

D/TAG: ffff

 

 

 

 

 

四.总结

TreeSet 有序 元素不可重复 元素不可为空 。没有get方法。

你可能感兴趣的:(Java,容器)