使用 Collections.frequency 找出重复的单词,,frequency 是
分享于 点击 43347 次 点评:153
使用 Collections.frequency 找出重复的单词,,frequency 是
frequency 是 Collections 的一个方法,可找出一个单词在list中出现的次数
[Java]代码
@SuppressWarnings("unchecked")public static void main(String[] args) {String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";List<String> list = new ArrayList<String>();list.addAll(Arrays.asList(text.split(" ")));Set<String> uniqueWords = new HashSet<String>(list);for (String word : uniqueWords) {System.out.println(word + ": " + Collections.frequency(list, word));}}
执行结果
ft: 1f: 7g: 2d: 5e: 1b: 1c: 1a: 3wed: 1sd: 1se: 1j: 3ws: 1k: 2h: 2w: 1v: 1s: 4r: 1gh: 1x: 1
用户点评