我们将创建一个NavigableSet集合并添加一些元素-
NavigableSet<String> set = new TreeSet<>(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU");
现在,将其descendingSet()
用于NavigableSet的反向操作-
NavigableSet<String>reverse = set.descendingSet();
import java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet<String> set = new TreeSet<>(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU"); NavigableSet<String>reverse = set.descendingSet(); System.out.println("NavigableSet = " + set); System.out.println("NavigableSet (Reverse) = " + reverse); } }
输出结果
NavigableSet = [ABC, DEF, GHI, JKL, MNO, PQR, STU] NavigableSet (Reverse) = [STU, PQR, MNO, JKL, GHI, DEF, ABC]