字数统计在Java中重复,它只计算字符重复计数字

时间:2015-06-12 11:34:06

标签: java count word

我的问题是Java计数中重复的字数重复字计数字符只在这里输入代码。

我只使用核心Java程序

tagShit

3 个答案:

答案 0 :(得分:0)

我认为这可能会对你有所帮助

    String name = "banana";
    Map<Character, Integer> countMap = new HashMap<>();
    name.chars()
.forEach((int i) -> countMap.put((char) i, countMap.getOrDefault((char) i, 0) + 1));

countMap.forEach((Character c,Integer count)->System.out.println("Character: "+c+" count: "+count));

或(我认为这将有更好的表现)

Map<Integer, Long> countMap2 = name.chars().boxed().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
countMap2.forEach((Integer c, Long count) -> {
        if (count > 1) {
            System.out.println("Character: " + (char) c.intValue() + " count: " + count);
        }
    });

答案 1 :(得分:0)

如果查询用于计算给定字符串中的重复单词,则代码为:

public static void getCount(String name) {
                java.util.StringTokenizer stoken = new java.util.StringTokenizer(name, " ");
                boolean flag = true;
                if (stoken.countTokens() > 1) {
                    java.util.Map<String, Integer> wordCountMap = new java.util.HashMap<String, Integer>();
                    while (stoken.hasMoreElements()) {
                        String str = stoken.nextElement().toString();
                        if (wordCountMap.containsKey(str)) {
                            wordCountMap.put(str, wordCountMap.get(str) + 1);
                        } else {
                            wordCountMap.put(str, 1);
                        }
                    }
                    System.out.println("Checking for Duplicates..");
                    for (String values : wordCountMap.keySet()) {
                        if (wordCountMap.get(values) > 1) {
                            flag = false;
                            System.out.println(values + "\t\t["+ wordCountMap.get(values) + "]");
                        }
                    }
                }
                if (flag) {
                    System.out.println("No duplicate words");
                }
            }

答案 2 :(得分:0)

程序输入一个句子并告诉返回最高频率的重复单词。请严格测试此代码。

import java.util.*;
public class DupsWords {

    public static String countDupsWords(String[] arr){
        Hashtable<String,Integer> ht = new Hashtable<String,Integer>(); 
        for(int i=0;i<arr.length;i++){
            if(ht.containsKey(arr[i])){
                ht.put(arr[i],ht.get(arr[i])+1);
            } else{
                ht.put(arr[i],1);
            }       
        }   
        Set<String> keys=ht.keySet();
        String result=null;
        int max=0;
        for(String itr : keys){
            if(Integer.parseInt(ht.get(itr).toString())>max){
                max=Integer.parseInt(ht.get(itr).toString());
                if((ht.contains(max))&&(max>1)){
                    result=itr;
                }       
            }               
        }
        if(result ==null){
            return "No Duplicate";
        }
        else{
            System.out.print("count is "+ max  +" for ");
            return ("'"+ result +"'" + " as a duplicate word");
        }
    }   

    public static void main(String args[ ]){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the String");
        String s= scan.nextLine();      
        String[] arr=s.split(" ");
        System.out.print(countDupsWords(arr));
    }

}