e358. creating a sorted set.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 26 行
TXT
26 行
A sorted set is a set that maintains its items in a sorted order. Inserts and retrievals are more expensive in a sorted set but iterations over the set is always in order.
See also e352 Creating a Set.
// Create the sorted set
SortedSet 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();
}
// The elements are iterated in order: a, b, c
// Create an array containing the elements in a set (in this case a String array).
// The elements in the array are in order.
String[] array = (String[])set.toArray(new String[set.size()]);
Related Examples
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?