前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Map统计字符串中字符出现的次数

利用Map统计字符串中字符出现的次数

作者头像
曼路
发布2018-10-18 15:28:45
1.4K0
发布2018-10-18 15:28:45
举报
文章被收录于专栏:浪淘沙浪淘沙

效果如下:

这里写图片描述
这里写图片描述
代码语言:javascript
复制
package cn.edu.nuc.map;

import java.util.HashMap;
import java.util.Map;

public class Test2 {
    /**
     * 统计字符串中字符出现的次数
     * 1.对字符串进行切割
     * 2.遍历字符串
     * 3.创建map对象,通过字符查看map中是否含有该字符
     *   若有该字符,value++;否则,添加该字符,设置value为1
     * 4.遍历map 查看统计结果
     * @param args
     */
    public static void main(String[] args) {
        String str="hello tom hello jerry hello tom hello jiji hello tom";
        String[] strings = str.split("\\s");


        /**
         * 方法一 用getOrDefault来设置map
         */
        Map<String,Integer> map2 = new HashMap();
        for (String string : strings) {
            Integer integer = map2.getOrDefault(string, 0);
            integer++;
            map2.put(string, integer);
        }
        for (String string : map2.keySet()) {
            System.out.println(string+" "+map2.get(string));

        }
        System.out.println("------------------------------------------");
        /**
         * 方法二 if-else创建
         */
        Map<String,Integer> map1 = new HashMap();
        for (String string : strings) {
            Integer integer = map1.get(string);
            if(integer==null)
                map1.put(string,1);
            else {
                integer=integer+1;
                map1.put(string,integer);
            }

        }
        for (String string : map1.keySet()) {
            System.out.println(string+" "+map1.get(string));

        }
    }
}

二、从一个文件中输入数据,进行分割后,再输出到另一个文件中

代码语言:javascript
复制
public  class Test5{
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("d:/a/hello.txt");
        FileOutputStream fos = new FileOutputStream("d:/a/result.txt");
        //创建管道 用来接收数据
        StringBuffer strb = new StringBuffer();
        int len = 0;
        while((len=fis.read())!=-1) {
            strb.append(new String(new byte[] {(byte)len}));
        }
        System.out.println(strb);
        //将接收到的数据转换成字符串,分割 并存到map中
        String str = strb.toString();
        String[] split = str.split("\\s");
        Map<String,Integer> map = new HashMap<>();
       for (String string : split) {
           if(!map.containsKey(string)) {
               map.put(string, 1);
           }else {
               int value = map.get(string);
               map.put(string, value+1);
           }
    }
       //往文件里写入
       String s ="";
        for (String string : map.keySet()) {
            System.out.println(string+":"+map.get(string));
            s = s+string +":"+map.get(string)+";  ";
        }
        System.out.println(s);
        //将String转换成字节进行写入
        byte[] byte1 = s.getBytes();
        fos.write(byte1);
        fos.close();
        fis.close();

    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档