排序集是在其元素上有排序的集合。
SortedSet
接口表示Java集合中的排序集合框架。
排序集中的元素可以按照自然順序排序可比較的
接口或使用 Comparator
。
SortedSet
必須知道如何在添加元素時(shí)對(duì)其元素進(jìn)行排序檢查兩個(gè)接口:
如果指定了 Comparator
,則 Comparator
是用于排序并忽略 Comparable
接口。
TreeSet
類是Collection框架中SortedSet接口的一個(gè)實(shí)現(xiàn)。
在下面的代碼中,我們添加 String
對(duì)象 SortedSet
。
String
類實(shí)現(xiàn) Comparable
接口。
SortedSet將使用 Comparable
接口及其 compareTo()
方法對(duì)String值進(jìn)行排序。
import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { // Create a sorted set of some names SortedSet<String> sortedNames = new TreeSet<>(); sortedNames.add("Java"); sortedNames.add("SQL"); sortedNames.add("HTML"); sortedNames.add("CSS"); // Print the sorted set of names System.out.println(sortedNames); } }
上面的代碼生成以下結(jié)果。
以下代碼顯示如何存儲(chǔ)在 SortedSet
中的人物對(duì)象列表。
我們不能添加Person類的對(duì)象在SortedSet中,除非我們還提供一個(gè) Comparator
對(duì)象因?yàn)镻erson類不實(shí)現(xiàn) Comparable
接口。
以下代碼創(chuàng)建一個(gè) SortedSet
的使用 Comparator
的人使用他們的名字排序的人:
SortedSet<Person> personsSortedByName = new TreeSet<>(Comparator.comparing(Person::getName));
該代碼使用方法引用來創(chuàng)建用于創(chuàng)建Comparator對(duì)象的lambda表達(dá)式。
import java.util.Comparator; import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { SortedSet<Person> personsById = new TreeSet<>( Comparator.comparing(Person::getId)); personsById.add(new Person(1, "X")); personsById.add(new Person(2, "Z")); personsById.add(new Person(3, "A")); personsById.add(new Person(4, "C")); personsById.add(new Person(4, "S")); // A duplicate Person System.out.println("Persons by Id:"); personsById.forEach(System.out::println); SortedSet<Person> personsByName = new TreeSet<>( Comparator.comparing(Person::getName)); personsByName.add(new Person(1, "X")); personsByName.add(new Person(2, "Z")); personsByName.add(new Person(3, "A")); personsByName.add(new Person(4, "C")); System.out.println("Persons by Name: "); personsByName.forEach(System.out::println); } } class Person { private int id; private String name; public Person(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (!(o instanceof Person)) { return false; } // id must be the same for two Persons to be equal Person p = (Person) o; if (this.id == p.getId()) { return true; } return false; } @Override public int hashCode() { return this.id; } @Override public String toString() { return "(" + id + ", " + name + ")"; } }
上面的代碼生成以下結(jié)果。
SortedSet
接口繼承了 Set
接口的所有方法,并添加了一些方法來返回子集。
subSet(E fromElement,E toElement)
方法從 SortedSet
返回fromElement(包含)和toElement(exclusive)之間的元素。
import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { SortedSet<String> names = new TreeSet<>(); names.add("HTML"); names.add("Java"); names.add("SQL"); names.add("CSS"); System.out.println("Sorted Set: " + names); System.out.println("First: " + names.first()); System.out.println("Last: " + names.last()); SortedSet<String> ssBeforeCSS = names.headSet("CSS"); System.out.println(ssBeforeCSS); SortedSet<String> ssBetwenCSSAndHTML = names.subSet("CSS", "HTML"); System.out.println(ssBetwenCSSAndHTML); SortedSet<String> ssBetwenCSSAndHTML2 = names.subSet("CSS", "HTML"); System.out.println(ssBetwenCSSAndHTML2); SortedSet<String> ssCSSAndAfter = names.tailSet("CSS"); System.out.println(ssCSSAndAfter); } }
上面的代碼生成以下結(jié)果。
以下代碼片段使用 Comparator
創(chuàng)建一個(gè) SortedSet
,它將null元素放在第一位:
import java.util.Comparator; import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { // Sort the names based on their length, placing null first SortedSet<String> names = new TreeSet<>(Comparator.nullsFirst(Comparator .comparing(String::length))); names.add("XML"); names.add("CSS"); names.add("HTML"); names.add(null); // Adds a null // Print the names names.forEach(System.out::println); } }
上面的代碼生成以下結(jié)果。
更多建議: