2021-05-26

用HashMap实现对文章中每个单词出现次数的统计并按照出现次数降序排列

题目要求

题目是我们Java老师上课时即兴出的,要求用HashMap实现对文章中每个单词出现次数的统计并按照出现次数降序排列。

个人理解

一开始觉得还没什么难的,读入HashMap比较简单,但是HashMap没法排序,想了很久(也可能是自己水平不够)并从网上查阅了不少大神的资料,想到把HashMap的EntrySet放到一个List中再使用Collections.sort方法进行排序

源码

package IoStream.Exercise;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class WordsCount {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\ZYY\\Desktop\\哼哼\\yqy.txt"));
        //不要在意这个文件路径
        String line = "";
        HashMap<String,Integer> mapcount = new HashMap<>();
        while((line = br.readLine()) != null){
            String[] words = line.split(" ");
            for(String s : words){
                if(mapcount.containsKey(s)){
                    mapcount.put(s, mapcount.get(s)+1);
                }else{
                    mapcount.put(s,1);
                }
            }
        }
        List<Map.Entry<String,Integer>> list = new ArrayList<>(mapcount.entrySet());//将mapcount的entryset放入List中
        对list进行排序,并通过Comparator传入自定义的排序规则
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        Iterator<Map.Entry<String, Integer>> iterator = list.iterator();
        while(iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
            System.out.println(next.getKey() +":" + next.getValue());

        }
    }

}

输出案例

(因为文章中有标点符号 所以有些字母带着标点符号,这个问题我暂时无法解决,请大神们指正。)

you:26
to:15
who:10
the:8
those:8
have:7
want:6
that:6
and:6
of:6
make:6
your:4
when:4
in:4
a:4
one:4
life:4
with:4
them:3
people:3
their:3
just:3
on:3
will:3
appreciate:2
let:2
is:2
touched:2
don’t:2
are:2
so:2
only:2
things:2
everyone:2
this:2
hurts:2
were:2
for:2
around:2
miss:2
go:2
really:2
what:2
tried,for:1
do.:1
past,:1
bad:1
tear.The:1
go;be:1
sorrow:1
hope:1
kiss:1
hurt,:1
put:1
Dream:1
begins:1
lifeuntil:1
searched,and:1
you,it:1
pick:1
strong,enough:1
human,enough:1
crying.:1
if:1
they:1
based:1
happen:1
past:1
opportunity:1
it:1
others’shoes.If:1
something:1
way.Happiness:1
die,you’re:1
real!:1
Always:1
much:1
other:1
be:1
May:1
probably:1
comes:1
out:1
The:1
see:1
heartaches.:1
everything;they:1
where:1
happy?:1
everything:1
it,to:1
side:1
failures:1
too.:1
be,because:1
smiling.Live:1
message.:1
When:1
trials:1
future:1
don’t,:1
happiest:1
feel:1
do:1
lives.Love:1
born,you:1
smile:1
There:1
friendship.And:1
forgotten:1
from:1
yourself:1
day:1
all:1
always:1
smiling:1
need:1
another,to:1
Please:1
most:1
keep:1
know:1
send:1
smile,grows:1
importance:1
dreams:1
best:1
necessarily:1
you,you:1
can:1
moments:1
along:1
happiness:1
brightest:1
crying:1
ends:1
lies:1
down,to:1
dream;go:1
sweet,enough:1
brighten:1
chance:1
or:1
you,to:1
brighter:1
was:1
enough:1
message:1
way:1
can’t:1
worry,nothing:1
someone:1
mean:1
person,:1
someone’s:1
well:1
hug:1
cry,those:1
进程已结束,退出代码为 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值