开发者

count number of occurences from a text file-using map

开发者 https://www.devze.com 2023-01-29 13:32 出处:网络
The code below will count the occurences of every character. If i have abc in the text file output would be a 1 b 1 c 1. I read in many sites that for loop will be taking much of time and it is better

The code below will count the occurences of every character. If i have abc in the text file output would be a 1 b 1 c 1. I read in many sites that for loop will be taking much of time and it is better to implement the sam开发者_运维百科e using hash map. Can any of you help me how to convert this program implementing hash map?

 import java.io.*;

    class Count_Char {
    public static void main(String[] args) {
        try
        {
    FileInputStream file = new FileInputStream("D:\\trial.txt");
    DataInputStream dis = new DataInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    String Contents="";
    String str="";
    while ((Contents = br.readLine()) != null) {
    str+=Contents;
    }
    char[]char_array =str.toCharArray();
    for(int count =0;count<char_array.length;count++){
    char ch= char_array[count];
    int counter=0;
    for ( int i=0; i<char_array.length; i++){
    if (ch==char_array[i])
    counter++;
    }
    boolean flag=false;
    int j=count-1;
    while(j>=0)
        {

        if(ch==char_array[j])
            flag=true;
            j--;
        }
    if(!flag){
    System.out.println(ch+" "+counter);
    }
    }
        }catch(IOException e1){
            System.out.println(e1);
        }
        }
    }


Quick pseudo code. Basically the trick here is that you save the characters as keys in the Map and the value being the count of the occurrences for that character (key/value pair).

 //declare a map to hold your characters and their counters
 Map<String,Integer> charCounter = new HashMap<String,Integer>();
 //the following if else logic goes when you are looping through your tokens
    if(charCounter.containsKey(<your character>)){
           charCounter.put(<your character>,charCounter.get(<your character>)+1);
    }else{
          charCounter.put(<your character>,1);
    }

After you are done traversing, you can print the map this way.

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


FileInputStream file = new FileInputStream(""); DataInputStream dis = new DataInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(dis));

        String temp="";
        Map<String,Integer> charCounter = new HashMap<String,Integer>();
      while ((temp=br.readLine()) != null)
      {
         String[] spliter= temp.split("");
         for(String temp1:spliter)
         if(charCounter.containsKey(temp1)){
               charCounter.put(temp1,charCounter.get(temp1)+1);
        }else{
              charCounter.put(temp1,1);
        }


      }


            System.out.println(charCounter);

build on logic coolbean gave hav writtn it ..Hope this will help... I have tested this..

0

精彩评论

暂无评论...
验证码 换一张
取 消