[Java 集合框架]统计下面这篇文章and,for,you,us出现个数并按照如下要求输出单词(出现的次数)例如: and(x), for(y), us(m), yo

统计下面这篇文章and,for,you,us出现个数并按照如下要求输出单词(出现的次数)
例如: and(x), for(y), us(m), you(z)

On Friendship
and a youth said, “Speak to us of Friendship.”

Your friend is your needs answered.

He is your field which you sow with love and reap with thanksgiving.

and he is your board and your fireside.

for you come to him with your hunger, and you seek him for peace.

When your friend speaks his mind you fear not the “nay” in your own mind, nor do you withhold the “ay.”

and when he is silent your heart ceases not to listen to his heart;

for without words, in friendship, all thoughts, all desires, all expectations are born and shared, with joy that is unacclaimed.

1.分析实现方法
返回值类型:String:and(x), for(y), us(m), you(z)
参数列表:String articlel,String… words
方法名:getWordsCountInArticle

2.编写代码

public class Demo04 {
    public static void main(String[] args) {
       //8.统计下面这篇文章and,for,you,us出现个数并按照如下要求输出
        //单词(出现的次数)例如:  and(x), for(y), us(m), you(z)
       String article= "On Friendship \n" +
               "and a youth said, \"Speak to us of Friendship.\" \n" +
               "\n" +
               "Your friend is your needs answered. \n" +
               "\n" +
               "He is your field which you sow with love and reap with thanksgiving. \n" +
               "\n" +
               "and he is your board and your fireside. \n" +
               "\n" +
               "for you come to him with your hunger, and you seek him for peace. \n" +
               "\n" +
               "When your friend speaks his mind you fear not the \"nay\" in your own mind, nor do you withhold the \"ay.\" \n" +
               "\n" +
               "and when he is silent your heart ceases not to listen to his heart; \n" +
               "\n" +
               "for without words, in friendship, all thoughts, all desires, all expectations are born and shared, with joy that is unacclaimed.";

        System.out.println(getWordsCountInArticle(article, "and", "for", "us", "you"));//可以动态填加单词查找

    }
    //返回值类型: String: and(x), for(y), us(m), you(z)
    //参数列表: String article, string ... words
    //方法名: getWordsCountInArticle
    public static String getWordsCountInArticle(String article,String... words){
        //定义容器,保存单词出现次数
        TreeMap<String,Integer> tm = new TreeMap<>();
        //定义容器,保存文章中符合的单词
        List<String> list = new ArrayList<>();
        //定义容器,存储正则表达式字符串
        StringBuilder sb = new StringBuilder();
        //动态拼接正则表达式
        //要查找的内容在main中填写
        sb.append("\\b(");
        for (int i = 0; i < words.length; i++) {
            if (i == words.length - 1) {
                sb.append(words[i]);
            }else {
                sb.append(words[i]).append("|" );
            }
        }
        sb.append(")\\b");

        Pattern p  = Pattern.compile(sb.toString());
        Matcher m = p.matcher(article);
        while (m.find()) {
            list.add(m.group());
        }
		
		//遍历list
        for (String s : list) {
            Integer count = tm.get(s);
            if (count == null) {
                tm.put(s, 1);
            }else {
                tm.put(s, ++count);
            }
        }

        //清空StringBuilder
        sb.delete(0,sb.length());
		
		//打印结果
        int index = 0;
        for (String key : tm.keySet()) {
        //如果是最后一个单词的话这样打印
            if (index == tm.size() - 1) {
                sb.append(key).append("(").append(tm.get(key)).append(")");
            }else {
                sb.append(key).append("(").append(tm.get(key)).append(")").append(" , ");
            }//否则在最后再加上一个逗号
            index ++;
        }
        return sb.toString();
    }
}

3.运行结果

and(7) , for(3) , us(1) , you(5)

Process finished with exit code 0

4.总结:
这个题相对来说比较综合,不要上来直接开始写代码,可以按照上面三个分析顺序先分析一下,一步一步编写代码,写完一段可以运行一下检查输出的结果是否为自己所预期的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值