云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二十一章练习题答案

《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二十一章练习题答案

  • 62 次阅读
  • 3 次下载
  • 2025/6/27 11:36:11

Integer max = Collections.max(treeMap.values()); Set keys = treeMap.keySet();

Iterator iterator = keys.iterator();

while (iterator.hasNext()) { Object key = iterator.next();

Integer value = (Integer)(treeMap.get(key)); if (value.equals(max)) {

System.out.println(\ + key + \); } } } }

21.7

import java.util.*;

public class Exercise21_07 {

public static void main(String[] args) { // Text in a string

String text = \ + \;

// Create a hash map to hold words and key and count as value HashMap hashMap = new HashMap<>();

String[] tokens = text.split(\); for (String key: tokens) { if (hashMap.get(key) != null) {

hashMap.put(key, hashMap.get(key).intValue() + 1); } else {

if (key.trim().length() > 0) hashMap.put(key, 1); } }

// Get an entry set for the tree map

Set> entrySet = hashMap.entrySet();

ArrayList list = new ArrayList<>();

for (Map.Entry entry: entrySet) {

list.add(new WordOccurrence(entry.getKey(), entry.getValue())); }

Collections.sort(list);

for (WordOccurrence item: list) { System.out.println(item); } } }

class WordOccurrence implements Comparable { String word; int count;

public WordOccurrence(String word, int count) { this.word = word; this.count = count; }

@Override

public int compareTo(WordOccurrence o) { return count - o.count; }

@Override

public boolean equals(Object o) {

return word.equals(((WordOccurrence)o).word); }

@Override

public String toString() { return word + \ + count; } }

21.8

import java.util.*; import java.io.*;

public class Exercise21_08 {

public static void main(String[] args) { if (args.length != 1) {

System.out.println(

\); System.exit(1); }

String filename = args[0];

// Create a tree map to hold words as key and count as value TreeMap treeMap = new TreeMap();

try {

Scanner input = new Scanner(new File(filename));

while (input.hasNext()) {

String line = input.nextLine();

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

for (int i = 0; i < words.length; i++) { if (words[i].trim().length() > 0 && words[i].trim().matches(\)) {

if (treeMap.get(key) != null) { int count = treeMap.get(key); count++;

treeMap.put(key, count); } else {

treeMap.put(key, 1); }

String key = words[i].toLowerCase();

} } } }

catch (Exception ex) { ex.printStackTrace(); }

// Get an entry set for the tree map

Set> entrySet = treeMap.entrySet();

// Display words in alphabetical order

System.out.println(\ + \);

for (Map.Entry entry: entrySet)

System.out.println(entry.getValue() + \ + entry.getKey()); } }

21.9

import java.util.*;

public class Exercise21_09 {

public static void main(String[] args) { String[][] stateCapital = { {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \},

{\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \}, {\, \},

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

Integer max = Collections.max(treeMap.values()); Set keys = treeMap.keySet(); Iterator iterator = keys.iterator(); while (iterator.hasNext()) { Object key = iterator.next(); Integer value = (Integer)(treeMap.get(key)); if (value.equals(max)) { System.out.println(\ + key + \); } } } } <

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com