⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uniquewords21.java

📁 Thinking In Java 第四版练习题答案
💻 JAVA
字号:
// holding/UniqueWords21.java
// TIJ4 Chapter Holding, Exercise 21, page422
/* Using a Map<String,Integer>, follow the form of UniqueWords.java to create a
* program that counts the occurrence of words in a file. Sort the results using 
* Collections.sort() with a second argument of String.CASE_INSENSITIVE_ORDER (to
* produce an alphabetic sort), and display the result.
*/
import java.util.*;
import net.mindview.util.*;

public class UniqueWords21 {
	public static void main(String[] args) {
		List<String> words = new ArrayList<String>(
			new TextFile("SetOperations.java", "\\W+"));
		System.out.println("Words to count: " + words);
		Collections.sort(words, String.CASE_INSENSITIVE_ORDER);
		Map<String,Integer> wordCount =
			new LinkedHashMap<String,Integer>();
		Iterator it = words.iterator();
		int totalWords = 0;
		while(it.hasNext()) {
			String s = (String)it.next();
			if(words.contains(s)) {
				Integer count = wordCount.get(s);
				wordCount.put(s,
					count == null ? 1 : count + 1);
				totalWords++;
			}
		}
		System.out.println();
		System.out.println("Word count: " + wordCount);
		System.out.println();
		System.out.println("Total words: " + totalWords);		
	}		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -