从使用文本文件的映射计算出现次数 [英] count number of occurences from a text file-using map

查看:99
本文介绍了从使用文本文件的映射计算出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将计算每个字符的出现次数。如果我有abc在文本文件输出将是1 b 1 c 1.我在许多站点读了for循环将花费大量的时间,最好是使用哈希映射实现相同。你可以帮我如何转换这个程序实现哈希映射吗?

  import java.io. *; 

类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 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);
}
}
}


解决方案>

快速伪码。基本上,这里的技巧是将字符保存为Map中的键,值是该字符(键/值对)出现次数的计数。

  //声明一个地图来保存您的字符及其计数器
Map< String,Integer> charCounter = new HashMap< String,Integer>();
//如果else循环通过你的令牌,
if(charCounter.containsKey(< your character>)){
charCounter.put(< your character> ,charCounter.get(< your character>)+ 1);
} else {
charCounter.put(< your character>,1);
}

完成遍历后,可以以这种方式打印地图。 p>

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


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 same 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));
}

这篇关于从使用文本文件的映射计算出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆