java 面试题整理一

题目:有一段英文内容,请统计每个单词出现的次数,例如

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit dicta eveniet perferendis molestiae quasi consectetur, corporis omnis quas voluptatibus consequatur explicabo, aut nulla inventore iusto voluptatem eaque, hic sint doloribus repudiandae, ea voluptatum quibusdam. Illum velit corporis, quibusdam, beatae, laborum vel deleniti molestiae sit alias sed, placeat ipsam cupiditate. Esse magni quam laudantium similique cum ratione quia, voluptatem animi nobis molestias labore aspernatur perferendis quidem omnis quasi minima adipisci laboriosam nihil! Atque corrupti molestias, dolorum eos quas rem fugit exercitationem facere laudantium ea inventore dolorem, aliquid illum aperiam. Maiores sit officia temporibus mollitia aliquam fugiat, consectetur iusto fuga perspiciatis repudiandae .et..

以上内容是在sublime text中利用lorem100生成的

思路:首先按单词进行拆分,然后将得到的单词作为key值保存到Map中,该key值对应的value值则为该单词在本段内容中出现的次数
分析:观察这段内容可知,除了单词外还有[空格]、[,+空格]、[.+空格]、[!+空格]、[空格+.]、[..],这些内容都需要作为分隔符来拆分这段内容

代码:

public class StringDemo {

    public static void main(String[] args){
        String s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit dicta eveniet perferendis" +
                " molestiae quasi consectetur, corporis omnis quas voluptatibus consequatur explicabo, aut nulla" +
                " inventore iusto voluptatem eaque, hic sint doloribus repudiandae, ea voluptatum quibusdam. Illum" +
                " velit corporis, quibusdam, beatae, laborum vel deleniti molestiae sit alias sed, placeat ipsam" +
                " cupiditate. Esse magni quam laudantium similique cum ratione quia, voluptatem animi nobis" +
                " molestias labore aspernatur perferendis quidem omnis quasi minima adipisci laboriosam nihil!" +
                " Atque corrupti molestias, dolorum eos quas rem fugit exercitationem facere laudantium" +
                " ea inventore dolorem, aliquid illum aperiam. Maiores sit officia temporibus mollitia aliquam" +
                " fugiat, consectetur iusto fuga perspiciatis repudiandae .et..";
        //多个分隔符的匹配顺序是从左至右,故将[空格]分割符放到了最后
        s = s.trim();
        String[] strArray = s.split(", |\\. |! | \\.|\\.\\.| ");

        Map<String,Integer> wordMap = new HashMap<String,Integer>(100);
        int frequency = 0;

        for(String str : strArray){
            if(wordMap.containsKey(str)){
                frequency = wordMap.get(str);
                frequency++;
                wordMap.put(str,frequency);
            }else{
                wordMap.put(str,1);
            }
        }

        Iterator<Map.Entry<String,Integer>> iterator = wordMap.entrySet().iterator();
        Map.Entry entry = null;
        while(iterator.hasNext()){
            entry = iterator.next();
            System.out.println("\""+entry.getKey()+"\""+":"+entry.getValue());
        }

    }
}

补充一种思路:
将所有的符号全部转换为空格,然后再根据空格直接拆分

String testS = ",,!2.? 3  4";
testS = testS.replace(","," ").replace("!"," ")
        .replace("."," ").replace("?","");
System.out.println(testS);
//此处的正则表达式中的+号不能少,此处用于将多个连续的空格替换成一个空格
testS = testS.replaceAll(" +"," ");
System.out.println(testS);
testS = testS.trim();
String[] testArray = testS.split(" ");
for(String testStr : testArray){
    System.out.println(testStr);
}

输出结果为

   2  3  4
 2 3 4
2
3
4

大家有其它方式欢迎补充!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值