アプリ版:「スタンプのみでお礼する」機能のリリースについて

List1.txtの内容の単語の数を数え、その数を単語の隣に表示するプログラムを作りました。


(1)List1.txtの内容
ゲーム
ゲーム
麻雀
麻雀
野球
ゲーム


(2)実行結果
C:\>java Lists
ゲーム 3
野球 1
麻雀 2


(3)プログラム
import java.io.BufferedWriter;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.*;


public class Lists {
public static void main(String[] args) throws IOException{

File file3 = new File("C:\\List1.txt");
BufferedReader br3 = new BufferedReader(new FileReader(file3));
FileWriter filewriter3 = new FileWriter(file3,true);

TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
String line;
while((line = br3.readLine()) != null){

String[] words = line.split("\\s");

for(String s : words){
if(!tm.containsKey(s)){
tm.put(s,1);
}else{
tm.put(s,tm.get(s).intValue()+1);
}} }

for(String s : tm.keySet()){
System.out.println(s + " " + tm.get(s) );
}}}



このプログラムを上から単語の数が多い順に表示したいのですがどこをどう修正したらいいのか悩んでます。

C:\>java Lists
ゲーム 3
麻雀 2
野球 1

と表示されればOKです。

よろしくお願いします。

A 回答 (2件)

Lists.java



import java.io.BufferedWriter;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.*;


public class Lists {
public static void main(String[] args) throws IOException{

File file3 = new File("C:\\List1.txt");
BufferedReader br3 = new BufferedReader(new FileReader(file3));
FileWriter filewriter3 = new FileWriter(file3,true);

TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
String line;
while((line = br3.readLine()) != null){

String[] words = line.split("\\s");

for(String s : words){
if(!tm.containsKey(s)){
tm.put(s,1);
}else{
tm.put(s,tm.get(s).intValue()+1);
}
}
}

for(String s : tm.keySet()){
System.out.println(s + " " + tm.get(s) );
}

TreeMap<String,String> tm2 = new TreeMap<String,String>(new ExmComparator());

for(String s : tm.keySet()){
tm2.put(tm.get(s)+" "+s," ");
}

for(String s : tm2.keySet()){
System.out.println(s);
}
}
}

ExmComparator.java

//降順に並べるためのコンパレータ
public class ExmComparator implements java.util.Comparator{
public int compare( Object object1, Object object2 ){
return ( (Comparable)object1 ).compareTo( object2 ) * -1;
}
}

結果
ゲーム 3
野球 1
麻雀 2
3 ゲーム
2 麻雀
1 野球
    • good
    • 0
この回答へのお礼

大変参考になりました。ありがとうございます。

お礼日時:2014/01/20 20:09

// 対象のマップ


Map<String,Integer> statistic = (中略);

// マップ要素で、配列を作成
List<Map.Entry<String,Integer>> data = new ArrayList<>(statistic.entrySet());

// 配列をソートして取得
Collections.sort(data, new Test());
for (Map.Entry<String,Integer> entry: data) {
System.out.printf("%4d %s\n", entry.getValue(), entry.getKey());
}

(中略)

// マップ要素の値で降順、となる比較関数
class Test implements Comparator<Map.Entry<String,Integer>> {
public int compare(Map.Entry<String,Integer> a, Map.Entry<String,Integer> b) {
return -(b.getValue() - a.getValue());
}
}
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!