//利用HashSet进行词频统计 package work; import java.util.Arrays; import java.util.HashSet; public class Deam0 { public static void main(String[] args) { String str[] = {"hello", "world", "world", "ok", "ok"}; //HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。 HashSet<String> os = new HashSet<String>(Arrays.asList(str)); System.out.println(os); //遍历os里的元素 for (String o : os) { int num=0; //遍历str里的元素 for (String s : str) { //判断os里的遍历出的元素和str里遍历出的元素是否相等 if(s == o){ //相等则num=num+1 num++; } } System.out.println("["+o+":"+num+"]"); } } }
09-26
1953
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-16
368
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)