免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
タイトル:
そう、次の3つの都市の前に番号記号を数える以上の都市のidの8プラス、23112411-北京の名前を入力し、同じ都市に数回、同じ市内でIDチェックが一度だけ同じ回数を覚えて、街の初回アルファベット順。
例:
入力:
34839946-beijing
34839934-beijing
34839946-beijing
34839946-shanghai
34839912-hangzhou
-1
出力:
city=beijing, citycount=2
city=hangzhou, citycount=1
city=shanghai, citycount=1
アイデア:
HashMapのことでは、入力データのフィルタリングの符号を繰り返し、その後、チェックインのすべての都市の数をカウントし、最終的にはHashMapの中のデータをソートHashMapを使用します。多くのオプションは、並び替えがあります:
①バブルソート、都市名を格納するための新しいString配列、HashMapののインデックス値によって配列のソート
②Collections.sortによって並べ替え
詳細な使用Collections.sortリンクをします。https://www.cnblogsは、 .COM / yw0219 / P / 7222108.html?は、utm_source = itdadao&utm_medium =紹介
コード:Collections.sortのソート
public class Mafengwo1 implements Comparable<Mafengwo1> {
private String city;
private int citycount;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getCitycount() {
return citycount;
}
public void setCitycount(int citycount) {
this.citycount = citycount;
}
public Mafengwo1(String city, int citycount) {
this.city = city;
this.citycount = citycount;
}
public Mafengwo1() {
}
static Map<String, Integer> map = new HashMap<String, Integer>();
static Map<String, Integer> map2 = new HashMap<String, Integer>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = new String();
str = in.next();
while (!str.equals("-1")) {
map.put(str, 1);
str = in.next();
}
in.close();
Mafengwo1 test = new Mafengwo1();
test.city(map);
}
public void city(Map<String, Integer> map) {
for (String str : map.keySet()) {
String str2 = new String();
str2 = str.substring(9);
map2.put(str2, map2.getOrDefault(str2, 0) + 1);
}
int i = 0;
List<Mafengwo1> empList = new ArrayList<>();
for (String k : map2.keySet()) {
Mafengwo1 emp = new Mafengwo1(k, map2.get(k));
empList.add(emp);
}
Collections.sort(empList, Comparator.reverseOrder());
while(i<3) {
System.out.println(empList.get(i));
i++;
}
}
@Override
public String toString() {
return " city=" + city + ", citycount=" + citycount;
}
@Override
public int compareTo(Mafengwo1 o) {
if (this.getCitycount() - o.getCitycount() == 0) {
return o.getCity().compareTo(this.getCity());
}
return this.getCitycount() - o.getCitycount();
}
}