不打印递增键频率的hashmap值

km0tfn4u  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(303)

我目前正在做一个项目来计算文本文件中单词的频率。驱动程序将单词放入arraylist(在将它们设置为小写并删除空格之后),然后freqcount对象将arraylist放入一个hashmap,该hashmap将处理频率操作。到目前为止,我可以让驱动程序读取文本文件,将其放入arraylist,然后将其放入hashmap。我的问题是hashmap节点不重复,所以每次看到单词时,我都尝试增加值。
司机:

package threetenProg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;

public class Driver {

    public static void main(String[] args) throws FileNotFoundException{
        File in = new File("test.txt");
        Scanner scanFile = new Scanner(in);

        ArrayList<String> parsed = new ArrayList<String>();

        while(scanFile.hasNext()) { //if this ends up cutting off bottom line, make it a do while loop
            parsed.add(scanFile.next().toLowerCase());
        }

        for(int i = parsed.size()-1; i>=0; i--) { //prints arraylist backwards
            System.out.println(parsed.get(i));
        } //*/

        FreqCount fc = new FreqCount(parsed);

        System.out.println("\n Hashmap: \n");
        fc.printMap();

        scanFile.close();

    }
}

频率计数:

package threetenProg3;

import java.util.HashMap;
import java.util.List;

public class FreqCount {
    //attributes and initializations
    private HashMap<String, Integer> map = new HashMap<String, Integer>();

    //constructors

    FreqCount(List<String> driverList){
        for(int dLIndex = driverList.size()-1; dLIndex>=0; dLIndex--) { //puts list into hashmap

            for(String mapKey : map.keySet()) {

                if(mapKey.equals(driverList.get(dLIndex))) {
                    int tval = map.get(mapKey);

                    map.remove(mapKey);

                    map.put(mapKey, tval+1);
                }else {
                    map.put(mapKey, 1);
                }

            }

        }

    }

    //methods

    public void printMap() {
        for (String i : map.keySet()) { //function ripped straight outta w3schools lol
              System.out.println("key: " + i + " value: " + map.get(i));
        }
    } //*/

}

文本文件:

ONE TWO ThReE FoUR fIve
six     seven        
EIGHT
     NINE       
TEN ELEVEN
ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE, ONE, ONE,

输出:

one,
one,
one,
one
one
one
one
one
one
one
one
one
one
one
one
one
one
eleven
ten
nine
eight
seven
six
five
four
three
two
one

 Hashmap: 

key: nine value: 1
key: one, value: 1
key: six value: 1
key: four value: 1
key: one value: 1
key: seven value: 1
key: eleven value: 1
key: ten value: 1
key: five value: 1
key: three value: 1
key: two value: 1
key: eight value: 1

据我所见,输出应该是打印正确的按键频率值。提前感谢您的帮助!

ma8fv8wu

ma8fv8wu1#

你可以改变 FreqCount 具体如下:

FreqCount(List<String> driverList) {
    for (int dLIndex = driverList.size() - 1; dLIndex >= 0; dLIndex--) {
        String key = driverList.get(dLIndex);
        if (map.get(key) == null) {
            map.put(key, 1);
        } else {
            map.put(key, map.get(key) + 1);
        }
    }
}

更改后的输出:

Hashmap: 

key: nine value: 1
key: one, value: 3
key: six value: 1
key: four value: 1
key: one value: 15
key: seven value: 1
key: eleven value: 1
key: ten value: 1
key: five value: 1
key: three value: 1
key: two value: 1
key: eight value: 1

或者,

FreqCount(List<String> driverList) {
    for (int dLIndex = driverList.size() - 1; dLIndex >= 0; dLIndex--) {
        String key = driverList.get(dLIndex);
        map.put(key, map.getOrDefault(key, 0) + 1);
    }
}
``` `Map#getOrDefault` 返回指定键Map到的值,如果此Map不包含该键的Map,则返回默认值。
sqougxex

sqougxex2#

imo,最简单的方法是使用map.merge方法。该方法接受上一个值并应用Map函数。在这种情况下,第二个值未使用。第一个值用于替换现有值+1。这样就得到了字符串出现的频率。
还请注意,我将我们的类改为使用 parse 返回Map的方法。在类构造函数中进行大量计算是不合适的。
在读入值之后。

FreqCount fc = new FreqCount();
Map<String,Integer> map = fc.parse(parsed);

map.entrySet().forEach(System.out::println);

印刷品

nine=1
one,=3
six=1
four=1
one=15
seven=1
eleven=1
ten=1
five=1
three=1
two=1
eight=1

修改后的类

class FreqCount {
    // attributes and initializations
    private Map<String, Integer> map =
            new HashMap<>();

    public Map<String, Integer> parse (List<String> driverList) {
        for (String str : driverList) {
            map.merge(str, 1, (v1,notUsed)->v1 + 1);
        }
        return map;
    }
}
nafvub8i

nafvub8i3#

你有几个问题。

FreqCount(List<String> driverList){
    for(int dLIndex = driverList.size()-1; dLIndex>=0; dLIndex--) { //puts list into hashmap
        if(map.get(driverList.get(dLIndex)) != null) {
            int tval = map.get(driverList.get(dLIndex));
            map.remove(driverList.get(dLIndex));
            map.put(driverList.get(dLIndex), tval+1);
        }else {
            map.put(driverList.get(dLIndex), 1);
        }
    }

}

你正在做一些奇怪的for循环,试图遍历一个空Map。您需要检查Map中是否有该单词的键,如果有,则向值中添加一个,如果没有,则添加值为1的新对。
为了避免使用comas或dots的数字(如果需要,可以向regex中添加replaceall作为参数的其他字符)

while(scanFile.hasNext()) { //if this ends up cutting off bottom line, make it a do while loop
        String value = scanFile.next().toLowerCase();
        value = value.replaceAll("[,.]", "");
        parsed.add(value);
    }

现在的输出是

one
one
one
one
one
one
one
one
one
one
one
one
one
one
one
one
one
eleven
ten
nine
eight
seven
six
five
four
three
two
one

 Hashmap: 

key: nine value: 1
key: six value: 1
key: four value: 1
key: one value: 18
key: seven value: 1
key: eleven value: 1
key: ten value: 1
key: five value: 1
key: three value: 1
key: two value: 1
key: eight value: 1

没有重复的话,即使昏迷和正确的计数每一个

相关问题