Java 简明教程
Java - SortedSet Interface
SortedSet interface 扩展 Set 并声明按升序排列的集合的行为。除了 Set 定义的那些方法之外,SortedSet 接口还声明在下表中总结的方法 -
The SortedSet interface extends Set and declares the behavior of a set sorted in an ascending order. In addition to those methods defined by Set, the SortedSet interface declares the methods summarized in the following table −
当调用集中不包含任何项时,若干方法会抛出 NoSuchElementException。当对象与集中元素不兼容时,会抛出 ClassCastException。
Several methods throw a NoSuchElementException when no items are contained in the invoking set. A ClassCastException is thrown when an object is incompatible with the elements in a set.
如果尝试使用 null 对象且集中不允许 null,则会抛出 NullPointerException。
A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the set.
SortedSet Interface Methods
Sr.No. |
Method & Description |
1 |
Comparator comparator( ) Returns the invoking sorted set’s comparator. If the natural ordering is used for this set, null is returned. |
2 |
Object first( ) Returns the first element in the invoking sorted set. |
3 |
SortedSet headSet(Object end) Returns a SortedSet containing those elements less than end that are contained in the invoking sorted set. Elements in the returned sorted set are also referenced by the invoking sorted set. |
4 |
Object last( ) Returns the last element in the invoking sorted set. |
5 |
SortedSet subSet(Object start, Object end) Returns a SortedSet that includes those elements between start and end.1. Elements in the returned collection are also referenced by the invoking object. |
6 |
SortedSet tailSet(Object start) Returns a SortedSet that contains those elements greater than or equal to start that are contained in the sorted set. Elements in the returned set are also referenced by the invoking object. |
Operations on SortedSet Interface
Creating a SortedSet
TreeSet class 实现 SortedSet 接口。我们可以使用 TreeSet 构造函数来创建一个 SortedSet 实例。以下是用创建 SortedSet 实例的语法:
TreeSet class implements the SortedSet interface. We can use the TreeSet constructor to create a SortedSet instance. Following is the syntax to create a SortedSet instance:
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
此处,我们正在创建一个字符串值的有序集合。此映射将存储唯一的字符串值。如果添加了重复值,则该值将被丢弃。
Here we’re creating a sorted set of String values. This map will store the unique string values. If a duplicate value is added, then that will be discarded.
Adding Value to a SortedSet
SortedSet 提供了一个 add() 方法,该方法可用于将值添加到 SortedSet 实例。每当将值添加到集合时,都会将其与现有值进行检查。如果集合已修改,则方法将返回 true,否则将返回 false。
SortedSet provides an add() method, which can be used to add value to a SortedSet instance. Whenever a value is added to the set, it is checked against the existing values. If the set is modified then method will return true otherwise false will be returned.
public boolean add(E e)
其中 E 表示要添加的元素。如果元素已存在,则不执行任何操作,并且方法将返回 false。
Where E represents the element to be added. If element is already present, then no action will be performed and method will return false.
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
Getting value from a SortedSet
为了从 SortedSet 获取值,我们必须使用 iterator() 方法从 SortedSet 获取迭代器对象。一旦迭代器对象可用,就可以使用该对象来检索 SortedSet 中存在的价值。
In order to get values from a SortedSet, we’ve to get the iterator object from the SortedSet using the iterator() method. Once the iterator object is available then that object can be used to retrieve values present in the SortedSet.
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
Deleting a value from a SortedSet
使用 remove(value) 方法,我们可以轻松地删除 SortedSet 中存储的值/对象。
Using the remove(value) method, we can remove the value/object stored in the SortedSet easily.
public boolean remove(Object value)
如果值不在集合中,则它将返回 false,否则它将删除该值并返回 true。
if value is not present in the set, then it will return false otherwise it will remove the value and return true.
set.remove("a");
Iterating SortedSet
SortedSet 条目可以轻松浏览。SortedSet 提供了一个方法 iterator(),该方法提供了一个迭代器来浏览集合的所有条目。
SortedSet entries can be easily navigated. SortedSet provides a method iterator() which provides an iterator to navigate all the entries of the set.
public Iterator<E> iterator()
其中 E 是要迭代的对象的类型。
Where E is the type of objects to be iterated.
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
Examples of SortedSet Interface
Adding Element to a SortedSet Example
SortedSet 在 TreeSet 等各种类中实现了其实现。以下是带有 add 操作的 TreeSet 类示例−
SortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add operation−
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
}
}
a
b
c
Removing Element from a SortedSet Example
SortedSet 在 TreeSet 等各种类中实现了其实现。以下是具有 add 和 remove 操作的 TreeSet 类示例 −
SortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add and remove operation −
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
set.add("d");
set.add("e");
set.add("f");
// remove elements
set.remove("c");
set.remove("f");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
}
}
a
b
d
e
Clearing a SortedSet Example
SortedSet 在 TreeSet 等各种类中实现了其实现。以下是具有 add 和 clear 操作的 TreeSet 类示例 −
SortedSet have its implementation in various classes like TreeSet. Following is an example of a TreeSet class with add and clear operation −
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
public static void main(String[] args) {
// Create the sorted set
SortedSet<String> set = new TreeSet<>();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
set.add("d");
set.add("e");
set.add("f");
System.out.println(set);
// remove elements
set.clear();
System.out.println(set);
}
}
[a, b, c, d, e, f]
[]
Advantages of SortedSet Interface
-
SortedSet ensures that the map is always sorted in ascending order of the values. Whenever a key-value pair is added to the SortedSet, it is re-sorted
-
Being sorted and unique, SortedSet is very efficient in searches
-
We can customize the sorting mechanism by using a comparator on the value type.
Disadvantages of SortedSet Interface
-
As a SortedSet instance has to be sorted every time an entry is added or changed, it becomes a performance bottleneck where changes are very frequent. In such cases, SortedSet is not preferred.
-
As SortedSet maintains unique records only, we cannot use this collection where duplicate entries can occur in the data set.