统计一句话中重复字符、单词的个数,HashMap,Queue List

//统计一句话中重复字符的个数(Queue)-----------------------------
package day081702;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
/**
 * 统计一句话中各个字符的个数
 */
public class MapDemo01 {

    public static void main(String[] args) {
        String str = "good good study, day day up.";

        Queue queue = toQueue(str);//把一串字符串处理为只含有小写字母的队列Queue

        Map map = new HashMap();

        while(queue.size()>0){
            Character c = queue.poll();//取出首元素,并做删除操作
            if(!map.containsKey(c)){
                map.put(c, 1);
            }else{
                map.put(c, map.get(c)+1);
            }
        }
        System.out.println(map);

    }
    /**
     * 把一串字符串处理为只含有小写字母的Queue
     * (其他无关字符过滤掉)
     * @param str
     * @return Queue
     */
    public static Queue toQueue(String str){

        //存储26个小写字母
        List list26 = new ArrayList();
        for(int i=0;i<26;i++){
            Character c = (char)('a'+i);
            list26.add(c);
        }

        //一旦是小写字母,存入queue
        Queue queue = new LinkedList();
        for(int i=0;i//遍历每个字符
            if(list26.contains(c)){//如果26个字母包含该字符,把该字符存入queue
                queue.offer(c);
            }
        }

        return queue;
    }

}
//统计一句话中重复字符的个数(List)--------------------------------------
package day081702;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class MapDemo02 {

    public static void main(String[] args) {
        String str = "good good study,day,day,up.";


        List list = toList(str);//把一串字符串处理为只含有小写字母的List
        Map map = new HashMap();

        long start = System.currentTimeMillis();
        for(int i=list.size()-1;i>=0;i--){
            Character c = list.get(i);
            if(!map.containsKey(c)){
                map.put(c, 1);
            }else{
                map.put(c, map.get(c)+1);
            }
            list.remove(i);
        }
        System.out.println(map);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
    /**
     * 把一串字符串处理为只含有小写字母的List
     * (其他无关字符过滤掉)
     * @param str
     * @return List
     */
    public static List toList(String str){

        //存储26个小写字母
        List list26 = new ArrayList();
        for(int i=0;i<26;i++){
            Character c = (char)('a'+i);
            list26.add(c);
        }

        //一旦是小写字母,存入list
        List list = new ArrayList();
        for(int i=0;i//遍历每个字符
            if(list26.contains(c)){//如果26个字母包含该字符,把该字符存入list
                list.add(c);
            }
        }

        return list;
    }

}

//统计一句话中重复单词的个数--------------------------------------
package day081702;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
/**
 * 统计一句话中重复单词的个数
 */
public class MapDemo03 {

    public static void main(String[] args) {
        String str = "good good study,    day day up.";
        Queue queue = toQueue(str);//toQueue()方法把String转为只包含单词的数组(过滤掉空格和标点符号)

        /*
         * 循环队列,放入map
         */
        Map map = new LinkedHashMap();//LinkedHashMap可按顺序循环输出
        //Map map = new HashMap();//HashMap无顺序输出
        while(queue.size()>0){
            String key = queue.poll();//取出首元素,并做删除操作
            if(!map.containsKey(key)){//如果不包含该key,则装入key,计数1
                map.put(key, 1);
            }else{
                map.put(key, map.get(key)+1);//如果已经包含该key,则value增加1
            }
        }

        /*
         * 循环key
         */
        Set keySet = map.keySet();
        for(String setWord:keySet){//高级for循环
            String word = setWord;
            System.out.println(word+"的重复次数:"+map.get(word));
        }

        Iterator it = keySet.iterator();
        while(it.hasNext()){//用Iterator来循环
            String word = it.next();
            System.out.println(word+"的重复次数:"+map.get(word));
        }

        /*
         * 循环Entry
         */

        Set> entrySet = map.entrySet();
        for(Entry entry: entrySet){//高级for循环
            String word = entry.getKey();
            Integer num = entry.getValue();
            System.out.println(word+"重复次数:"+num);
        }

        Iterator> entryIt = entrySet.iterator();
        while(entryIt.hasNext()){//用Iterator来循环
            Entry tmpEntry = entryIt.next();
            String word = tmpEntry.getKey();
            Integer num = tmpEntry.getValue();
            System.out.println(word+"重复次数:::"+num);

        }


    }
    /**
     * 把String转为只包含单词的数组(过滤掉空格和标点符号)
     * @param str
     * @return Queue
     */
    public static Queue toQueue(String str){
        String regex = "\\,|\\.";
        str = str.replaceAll(regex, " ");//把逗号点号转为空格
        System.out.println(str);

        String regex2 = "\\ +";
        str = str.replaceAll(regex2, " ");//把多个空格转为1个空格
        System.out.println(str);

        String[] words = str.split(" ");//转为单词数组
        System.out.println(Arrays.toString(words));

        //数组转为队列queue
        Queue queue = new LinkedList();
        for(String word : words){
            queue.offer(word);
        }
        return queue;
    }

}

你可能感兴趣的:(java)