Java Collection Framework树集

TreeSet class used to store unique elements in ascending order. It is similar to HashSet except that it sorts the elements in the ascending order while HashSet doesn’t maintain any order.

TreeSet类用于按升序存储唯一元素。 它与HashSet相似,不同之处在于它按升序对元素进行排序,而HashSet不维护任何顺序。

Java TreeSet class implements the Set interface and use tree based data structure storage. It extends AbstractSet class and implements the NavigableSet interface. The declaration of the class is given below.

Java TreeSet类实现Set接口并使用基于树的数据结构存储。 它扩展了AbstractSet类并实现了NavigableSet接口。 该类的声明在下面给出。

TreeSet类声明 (TreeSet class Declaration)

public class TreeSet<E>extends AbstractSet<E>implements NavigableSet<E>, Cloneable, Serializable

重要事项 (Important Points)

  1. It stores the elements in ascending order.

    它按升序存储元素。

  2. It uses a Tree structure to store elements.

    它使用Tree结构存储元素。

  3. It contains unique elements only like HashSet.

    它仅包含像HashSet这样的唯一元素。

  4. It's access and retrieval times are quite fast.

    它的访问和检索时间都非常快。

  5. It doesn't allow null element.

    它不允许使用null元素。

  6. It is non synchronized.

    它是不同步的。

TreeSet构造函数 (TreeSet Constructors)

TreeSet()
TreeSet( Collection C )  
TreeSet( Comparator comp )
TreeSet( SortedSet ss )

示例:创建树集 (Example: Creating an TreeSet)

Lets create a TreeSet to store string elements. Here set is empty because we did not add elements to it.

让我们创建一个TreeSet来存储字符串元素。 这里set为空,因为我们没有向其中添加元素。

import java.util.*;
class Demo
{
  public static void main(String[] args)
  {   
    // Creating an TreeSet
    TreeSet< String> fruits = new TreeSet< String>();
    
    // Displaying TreeSet
    System.out.println(fruits);
  }
}

[]

[]

TreeSet方法 (TreeSet Methods)

MethodDescription
boolean add(E e)It adds the specified element to this set if it is not already present.
boolean addAll(Collection<? extends E> c)It adds all of the elements in the specified collection to this set.
E ceiling(E e)It returns the equal or closest greatest element of the specified element from the set, or null there is no such element.
Comparator<? super E> comparator()It returns comparator that arranged elements in order.
Iterator descendingIterator()It is used iterate the elements in descending order.
NavigableSet descendingSet()It returns the elements in reverse order.
E floor(E e)It returns the equal or closest least element of the specified element from the set, or null there is no such element.
SortedSet headSet(E toElement)It returns the group of elements that are less than the specified element.
NavigableSet headSet(E toElement, boolean inclusive)It returns the group of elements that are less than or equal to(if, inclusive is true) the specified element.
E higher(E e)It returns the closest greatest element of the specified element from the set, or null there is no such element.
Iterator iterator()It is used to iterate the elements in ascending order.
E lower(E e)It returns the closest least element of the specified element from the set, or null there is no such element.
E pollFirst()It is used to retrieve and remove the lowest(first) element.
E pollLast()It is used to retrieve and remove the highest(last) element.
Spliterator spliterator()It is used to create a late-binding and fail-fast spliterator over the elements.
NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)It returns a set of elements that lie between the given range.
SortedSet subSet(E fromElement, E toElement))It returns a set of elements that lie between the given range which includes fromElement and excludes toElement.
SortedSet tailSet(E fromElement)It returns a set of elements that are greater than or equal to the specified element.
NavigableSet tailSet(E fromElement, boolean inclusive)It returns a set of elements that are greater than or equal to (if, inclusive is true) the specified element.
boolean contains(Object o)It returns true if this set contains the specified element.
boolean isEmpty()It returns true if this set contains no elements.
boolean remove(Object o)It is used to remove the specified element from this set if it is present.
void clear()It is used to remove all of the elements from this set.
Object clone()It returns a shallow copy of this TreeSet instance.
E first()It returns the first (lowest) element currently in this sorted set.
E last()It returns the last (highest) element currently in this sorted set.
int size()It returns the number of elements in this set.
方法 描述
布尔值add(E e) 如果指定的元素尚不存在,则将其添加到该集合中。
布尔addAll(Collection <?扩展E> c) 它将指定集合中的所有元素添加到该集合中。
E上限(E e) 它从集合中返回指定元素的等于或最接近的最大元素,如果没有,则返回null。
比较器<? 超级E>比较器() 它返回按顺序排列元素的比较器。
迭代器降序Iterator() 它用于按降序迭代元素。
NavigableSetDescendingSet() 它以相反的顺序返回元素。
E层(E e) 它从集合中返回指定元素的等于或最接近的最小元素,如果没有,则返回null。
SortedSet headSet(E toElement) 它返回小于指定元素的元素组。
NavigableSet headSet(E toElement,包括布尔值) 它返回小于或等于(如果包含,则为true)指定元素的元素组。
E更高(E e) 它返回集合中指定元素中最接近的最大元素,如果没有,则返回null。
迭代器iterator() 它用于按升序迭代元素。
E低(E e) 它从集合中返回指定元素中最接近的最小元素,如果没有,则返回null。
E pollFirst() 它用于检索和删除最低(第一个)元素。
E pollLast() 它用于检索和删除最高(最后)元素。
分离器spliterator() 它用于在元素上创建后绑定和故障快速拆分器。
NavigableSet subSet(E fromElement,boolean fromInclusive,E toElement,boolean toInclusive) 它返回位于给定范围之间的一组元素。
SortedSet subSet(E fromElement,E toElement)) 它返回位于给定范围(包括fromElement和不包括toElement)之间的一组元素。
SortedSet tailSet(E fromElement) 它返回一组大于或等于指定元素的元素。
NavigableSet tailSet(E fromElement,包括布尔值) 它返回一组大于或等于(如果包含,则为真)指定元素的元素。
布尔contains(Object o) 如果此集合包含指定的元素,则返回true。
布尔isEmpty() 如果此集合不包含任何元素,则返回true。
布尔值remove(Object o) 如果存在,则用于从该集合中删除指定的元素。
无效clear() 它用于删除此集合中的所有元素。
对象clone() 它返回此TreeSet实例的浅表副本。
E first() 它返回此排序集中当前的第一个(最低)元素。
E last() 它返回此排序集中当前的最后一个(最高)元素。
int size() 它返回此集合中的元素数。

将元素添加到TreeSet (Add Elements To TreeSet)

Lets take an example to create a TreeSet that contains duplicate elements. But you can notice that it prints unique elements that means it does not allow duplicate elements.

让我们以创建包含重复元素的TreeSet为例。 但是您会注意到,它打印出唯一的元素,这意味着它不允许重复的元素。

import java.util.*;
class Demo{
  public static void main(String args[]){
    TreeSet<String> al=new TreeSet<String>();
    al.add("Ravi");
    al.add("Vijay");
    al.add("Ravi");
    al.add("Ajay");

    Iterator itr=al.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
  }
}

Ajay Ravi Vijay

阿杰·拉维·维杰(Ajay Ravi Vijay)

从树集中删除元素 (Removing Elements From the TreeSet)

We can use remove() method of this class to remove the elements. See the below example.

我们可以使用此类的remove()方法删除元素。 请参见以下示例。

import java.util.*;
class Demo{
  public static void main(String args[]){
    TreeSet<String> al = new TreeSet<String>();
    al.add("Ravi");
    al.add("Vijay");
    al.add("Ravi");
    al.add("Ajay");

    Iterator itr=al.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
    
    al.remove("Ravi");
    System.out.println("After Removing: "+al);
  }
}

Ajay Ravi Vijay After Removing: [Ajay, Vijay]

删除后的Ajay Ravi Vijay:[Ajay,Vijay]

在TreeSet中搜索元素 (Search an Element in TreeSet)

TreeSet provides contains() method that true if elements is present in the set.

TreeSet提供了contains()方法,如果集合中存在元素,则为true。

import java.util.*;
class Demo{
  public static void main(String args[]){
    TreeSet<String> al = new TreeSet<String>();
    al.add("Ravi");
    al.add("Vijay");
    al.add("Ravi");
    al.add("Ajay");

    Iterator itr=al.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
    
    boolean iscontain = al.contains("Ravi");
    System.out.println("Is contain Ravi: "+iscontain);
  }
}

Ajay Ravi Vijay Is contain Ravi: true

Ajay Ravi Vijay是否包含Ravi:true

以升序和降序遍历TreeSet (Traverse TreeSet in Ascending and Descending Order)

We can traverse elements of Treeset in both ascending and descending order. TreeSet provides descendingIterator() method that returns iterator type for descending traversing. See the below example.

我们可以按升序和降序遍历Treeset的元素。 TreeSet提供了descendingIterator()方法,该方法返回用于迭代遍历的迭代器类型。 请参见以下示例。

import java.util.*;
class Demo{
  public static void main(String args[]){
    TreeSet<String> al = new TreeSet<String>();
    al.add("Ravi");
    al.add("Vijay");
    al.add("Ravi");
    al.add("Ajay");
    System.out.println("Ascending...");
    Iterator itr=al.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
    System.out.println("Descending...");
    Iterator itr2=al.descendingIterator();
    while(itr2.hasNext()){
      System.out.println(itr2.next());
    }
  }
}

Ascending... Ajay Ravi Vijay Descending... Vijay Ravi Ajay

升序...阿杰·拉维·维杰降序...维杰·拉维·阿杰

翻译自: https://www.studytonight.com/java/treeset-in-collection-framework.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值