- package com.heima.demo;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class Demo {
- public static void main(String[] args) {
- TreeSet<String> ts = new TreeSet<>(Collections.reverseOrder());//定义一个集合存储字符串 倒序
- Scanner sc = new Scanner(System.in);
- while(true) {
- String line = sc.nextLine();
- if("end".equals(line)) //判断一下 是end就跳出循环
- break;
- else ts.add(line);
- }
- Iterator<String> it = ts.iterator(); //迭代器获取集合元素
- while(it.hasNext()) {
- System.out.println(it.next());
- }
- sc.close();
- }
- }
复制代码 |