Java中的SortedSet接口及用法示例

2021年3月27日17:37:00 发表评论 1,113 次浏览

存在于SortedSet接口中java.util程序包扩展了存在于收集框架。它是实现数学集的接口。该接口包含从Set接口继承的方法, 并添加了一个功能, 该功能将所有元素存储在此接口中, 以有序方式存储。

Set-TreeSet-SortedJava集合中的集合

在上图中, 可导航集合扩展了已排序集合的界面。由于集合不保留插入顺序, 因此可导航的集合界面提供了在集合中导航的实现。实现可导航集的类是TreeSet, 它是自平衡树的实现。因此, 该界面为我们提供了一种浏览该树的方式。

宣言:SortedSet接口声明为:

公共接口SortedSet扩展Set

排序集示例:

// Java program to demonstrate the
// Sorted Set
import java.util.*;
  
class SortedSetExample{
  
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
  
         // Adding elements into the TreeSet
         // using add()
         ts.add( "India" );
         ts.add( "Australia" );
         ts.add( "South Africa" );
  
         // Adding the duplicate
         // element
         ts.add( "India" );
  
         // Displaying the TreeSet
         System.out.println(ts);
  
         // Removing items from TreeSet
         // using remove()
         ts.remove( "Australia" );
         System.out.println( "Set after removing "
                            + "Australia:" + ts);
  
         // Iterating over Tree set items
         System.out.println( "Iterating over set:" );
         Iterator<String> i = ts.iterator();
         while (i.hasNext())
             System.out.println(i.next());
     }
}

输出如下:

[Australia, India, South Africa]
Set after removing Australia:[India, South Africa]
Iterating over set:
India
South Africa

注意:SortedSet的所有元素必须实现可比的界面(或被指定的比较器接受), 并且所有此类元素必须相互可比较。相互可比只是意味着两个对象彼此接受, 作为其compareTo方法的参数。

创建SortedSet对象

由于SortedSet是一个接口, 则无法创建SortedSet类型的对象。我们总是需要一个扩展此列表的类以创建一个对象。而且, 在引入泛型在Java 1.5中, 可以限制可以存储在SortedSet中的对象的类型。此类型安全集可以定义为:

// Obj是要存储在SortedSet中的对象的类型SortedSet <Obj> set = new TreeSet <Obj>();

在SortedSet上执行各种操作

由于SortedSet是一个接口, 因此只能与实现此接口的类一起使用。 TreeSet是实现SortedSet接口的类。现在, 让我们看看如何在TreeSet上执行一些常用的操作。

1.添加元素:为了将元素添加到SortedSet中, 我们可以使用add()方法。但是, 插入顺序不会保留在TreeSet中。在内部, 对于每个元素, 将按升序对值进行比较和排序。我们需要注意, 不允许重复元素, 所有重复元素都将被忽略。而且, Null值不被SortedSet接受。

// Java code to demonstrate
// the working of SortedSet
import java.util.*;
  
class GFG {
  
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
  
         // Elements are added using add() method
         ts.add( "A" );
         ts.add( "B" );
         ts.add( "C" );
         ts.add( "A" );
  
         System.out.println(ts);
     }
}

输出如下:

[A, B, C]

2.访问元素:添加元素后, 如果我们希望访问元素, 则可以使用内置方法, 例如contains(), 第一(), 持续()等

// Java code to demonstrate
// the working of SortedSet
  
import java.util.*;
class GFG {
  
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
  
         // Elements are added using add() method
         ts.add( "A" );
         ts.add( "B" );
         ts.add( "C" );
         ts.add( "A" );
  
         System.out.println( "Sorted Set is " + ts);
  
         String check = "D" ;
  
         // Check if the above string exists in
         // the SortedSet or not
         System.out.println( "Contains " + check
                            + " " + ts.contains(check));
  
         // Print the first element in
         // the SortedSet
         System.out.println( "First Value " + ts.first());
  
         // Print the last element in
         // the SortedSet
         System.out.println( "Last Value " + ts.last());
     }
}

输出如下:

Sorted Set is [A, B, C]
Contains D false
First Value A
Last Value C

3.删除值:可以使用以下命令从SortedSet中删除这些值:remove()方法.

// Java code to demonstrate
// the working of SortedSet
  
import java.util.*;
class GFG{
  
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
  
         // Elements are added using add() method
         ts.add( "A" );
         ts.add( "B" );
         ts.add( "C" );
         ts.add( "B" );
         ts.add( "D" );
         ts.add( "E" );
  
         System.out.println( "Initial TreeSet " + ts);
  
         // Removing the element b
         ts.remove( "B" );
  
         System.out.println( "After removing element " + ts);
     }
}

输出如下:

Initial TreeSet [A, B, C, D, E]
After removing element [A, C, D, E]

4.遍历SortedSet:有多种方法可以遍历SortedSet。最著名的是使用增强了for循环。

// Java code to demonstrate
// the working of SortedSet
   
import java.util.*;
class GFG
  { 
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
   
         // Elements are added using add() method
         ts.add( "C" );
         ts.add( "D" );
         ts.add( "E" );
         ts.add( "A" );
         ts.add( "B" );
         ts.add( "Z" );
   
         // Iterating though the SortedSet
         for (String value : ts)
             System.out.print(value
                              + ", " );
         System.out.println();
     }
}

输出如下:

A, B, C, D, E, Z, 

实现SortedSet接口的类是TreeSet。

树集:在集合框架中实现的TreeSet类是SortedSet接口的实现, 并且SortedSet扩展了设置界面。它的行为就像一个简单的集合, 不同之处在于它以排序的格式存储元素。 TreeSet使用树数据结构进行存储。对象按升序存储。但是我们可以使用方法按降序进行迭代TreeSet.descendingIterator()。让我们看看如何使用此类创建一个sortedset对象。

// Java program to demonstrate the
// creation of SortedSet object using
// the TreeSet class
  
import java.util.*;
  
class GFG {
  
     public static void main(String[] args)
     {
         SortedSet<String> ts
             = new TreeSet<String>();
  
         // Adding elements into the TreeSet
         // using add()
         ts.add( "India" );
         ts.add( "Australia" );
         ts.add( "South Africa" );
  
         // Adding the duplicate
         // element
         ts.add( "India" );
  
         // Displaying the TreeSet
         System.out.println(ts);
  
         // Removing items from TreeSet
         // using remove()
         ts.remove( "Australia" );
         System.out.println( "Set after removing "
                            + "Australia:" + ts);
  
         // Iterating over Tree set items
         System.out.println( "Iterating over set:" );
         Iterator<String> i = ts.iterator();
         while (i.hasNext())
             System.out.println(i.next());
     }
}

输出如下:

[Australia, India, South Africa]
Set after removing Australia:[India, South Africa]
Iterating over set:
India
South Africa

SortedSet接口的方法

以下是SortedSet接口中提供的方法。在此, " *"表示方法是设置界面.

方法 描述
*add(元素) 此方法用于将特定元素添加到集合中。仅当集合中不存在指定的元素时, 该函数才会添加该元素;否则, 如果集合中已存在该元素, 则该函数将返回False。
* addAll(集合) 此方法用于将上述集合中的所有元素附加到现有集合中。元素是随机添加的, 不遵循任何特定顺序。
*clear() 此方法用于从集合中删除所有元素, 但不删除集合。该集合的参考仍然存在。
compare() 此方法返回用于对该集合中的元素进行排序的比较器;如果此集合使用其元素的自然排序, 则返回null。
*contains(元素) 此方法用于检查Set中是否存在特定元素。
* containsAll(集合) 此方法用于检查集合是否包含给定集合中存在的所有元素。如果集合包含所有元素, 则此方法返回true;如果缺少任何元素, 则返回false。
first() 此方法返回此集合中存在的第一个(最低)元素。
hashCode() 此方法用于获取此Set实例的hashCode值。它返回一个整数值, 它是Set的此实例的hashCode值。
headSet(元素) 此方法返回的元素少于排序集中存在的元素。
*isEmpty() 此方法用于检查SortedSet是否为空。
last() 此方法返回集合中存在的last(最高)元素。
* delete(元素) 此方法用于从集合中删除给定的元素。如果Set中存在指定的元素, 则此方法返回True, 否则返回False。
* removeAll(集合) 此方法用于从集合中删除集合中存在的所有元素。如果此设置因调用而更改, 则此方法返回true。
* retainAll(集合) 此方法用于保留给定集合中提到的集合中的所有元素。如果此设置因调用而更改, 则此方法返回true。
* size() 此方法用于获取集合的大小。这将返回一个整数值, 表示元素的数量。
subSet(element1, element2) 此方法从包含element1和element2之间的元素的集合返回排序后的子集。
tailSet(元素) 此方法返回大于或等于排序集中存在的元素的元素。
* toArray() 此方法用于形成与Set相同元素的数组。
木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: