- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class Demo4_TreeSet {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
-
- // 定义TreeSet集合实现倒序排列
- TreeSet<String> set = new TreeSet<>(new Comparator<String>() {
- @Override
- public int compare(String s1, String s2) {
- return s2.compareTo(s1);
- }
- });
-
- // 录入数据
- System.out.println("请录入数据:");
-
- while (true) {
- String line = sc.nextLine();
- if ("end".equals(line)) {
- break;
- }
- set.add(line);
- }
-
- // 遍历集合获取数据
- for (String str : set) {
- System.out.print(str + " ");
- }
-
-
- }
- }
复制代码 |
|