之前写过一篇关于Map(HashMap)的文章,当时还不太清楚其他类型的map,现在TreeMap它来啦!
题目概述
问题描述:
小U在学习英文时,需要统计英文句子中每个单词的出现次数,并按照字母顺序将统计结果进行排序。请你帮助小U编写一个程序,统计输入的英文句子中每个单词的出现 次数,并将结果按字母顺序排序后输出。
样例:
输入:s="New to Python or choosing between Python 2 and Python 3 Read Python 2 or Python 3"
输出:['2:2', '3:2', 'New:1', 'Python:5', 'Read:1', 'and:1', 'between:1', 'choosing:1', 'or:2', 'to:1']
题目分析
题目讲到关键词 出现次数 ,这个会让我第一时间想到使用key-value对的map数据结构。所以我使用的是最为基础的HashMap,以此作为存储键值对的工具。
HashMap代码实现
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class Main {
public static List<String> solution(String s) {
String[] arr = s.split(" ");
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
List<String> result = new ArrayList<>();
for (Map.Entry<String, Integer> entry : list) {
result.add(entry.getKey() + ":" + entry.getValue());
}
return result;
}
public static void main(String[] args) {
System.out.println(solution("New to Python or choosing between Python 2 and Python 3 Read Python 2 or Python 3")
.equals(List.of("2:2", "3:2", "New:1", "Python:5", "Read:1", "and:1", "between:1", "choosing:1", "or:2",
"to:1")));
System.out.println(solution("hello world hello python").equals(List.of("hello:2", "python:1", "world:1")));
System.out.println(solution("the quick brown fox jumps over the lazy dog")
.equals(List.of("brown:1", "dog:1", "fox:1", "jumps:1", "lazy:1", "over:1",
"quick:1", "the:2")));
}
}
- 第10行:对输入的String以空格作为标识进行分割,变成String类型的数组。
- 第11行到第14行:创建HashMap,对单词和单词出现的次数进行存储。
- 第16到第21行:将Map中的所有键值
Map.Entry()对转换为list类型,然后对key进行排序操作。 - 第23到第26行:创建list类型的result,将排序好的键值对以
key:value的字符串形式存储到result中。 - 返回result,OVER!
这边比较不熟悉的方法是Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {},这个是对Collections.sort()方法的自定义。
以上是我用HashMap对这道题目的解答。AI帮我优化的代码使用了TreeMap,我将把代码附在下面。
TreeMap代码实现
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.TreeMap;
public class Main {
public static List<String> solution(String s) {
String[] arr = s.split(" ");
Map<String, Integer> map = new TreeMap<>(); // 使用 TreeMap 自动按字母顺序排序
for (int i = 0; i < arr.length; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
// 将结果格式化为题目要求的输出格式
List<String> result = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
result.add(entry.getKey() + ":" + entry.getValue());
}
return result;
}
public static void main(String[] args) {
System.out.println(solution("New to Python or choosing between Python 2 and Python 3 Read Python 2 or Python 3")
.equals(List.of("2:2", "3:2", "New:1", "Python:5", "Read:1", "and:1", "between:1", "choosing:1", "or:2",
"to:1")));
System.out.println(solution("hello world hello python").equals(List.of("hello:2", "python:1", "world:1")));
System.out.println(solution("the quick brown fox jumps over the lazy dog")
.equals(List.of("brown:1", "dog:1", "fox:1", "jumps:1", "lazy:1", "over:1", "quick:1", "the:2")));
}
}
TreeMap因为是通过红黑树实现,红黑树结构天然支持排序,默认情况下通过Key值的自然顺序进行排。
所以,当我们使用TreeMap来解决这种既需要key-value存储又需要对key进行排序的问题时,整个代码量就会大大减少,而且运作效率也会提高。它减少了对key进行排序这个步骤,自带了对key排序的功能。
小结
这次也没有带入具体数据模拟代码的路径,因为整个过程还是比较清晰明了的,主要是对HashMap和TreeMap的功能需要了解清楚,这样才能更有合理有效地使用它们。建议大家还是多做一些数据存储类型的题目,熟能生巧,多做才能总结规律。
AND,写完代码可以借助AI进行优化,可能会牵扯到类似的知识点。
下期见!