package com.tjrac_java_2;
import java.util.Scanner;
import java.util.TreeSet;
public class test3 {
public static void main(String[] args) {
TreeSet<City> tree=new TreeSet<City>();
Scanner sc=new Scanner(System.in);
int sum=sc.nextInt();
for (int i = 0; i < sum; i++) {
City city=new City();
city.setName(sc.next());
tree.add(city);
}
sc.close();
for (City city:tree) {
System.out.println(city.getName());
}
}
}
class City implements Comparable{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
@Override
public int compareTo(Object o) {
City city=(City) o;
if(this.name.hashCode()==city.name.hashCode()){
return -1;
}
return this.name.compareTo(city.name);
}
}
重写java中集合类treeset中的方法
最新推荐文章于 2023-08-23 15:42:59 发布