Java HashMap字符串,并计算每个单词的使用次数

Java HashMap字符串,并计算每个单词的使用次数,java,hashmap,concurrenthashmap,Java,Hashmap,Concurrenthashmap,下面的问题是Java语言 样本数据: 我有一个tokenizationString数组,其中包含与上面列表类似的单词,其中包含许多重复的单词 我必须将该字符串数组转换为hashmap,然后使用hashmap计算每个单词的使用次数(计算字符串数组中的重复值,但我必须使用hashmap相关方法) 我正在考虑这样做 Map<Integer, String> hashMap = new HashMap<Integer, String>(); for

下面的问题是Java语言

样本数据:

我有一个tokenizationString数组,其中包含与上面列表类似的单词,其中包含许多重复的单词

我必须将该字符串数组转换为hashmap,然后使用hashmap计算每个单词的使用次数(计算字符串数组中的重复值,但我必须使用hashmap相关方法)

我正在考虑这样做

Map<Integer, String> hashMap = new HashMap<Integer, String>();    
            for(int i = 0 ; i < tokenizationString.length; i++)
                {
                   hashMap.put(i, tokenizationString[i]);

                }
而不是

hashMap.put(i, tokenizationString[i]);
首先检查单词是否已经存在,然后增加相应的条目:

int count = hashMap.containsKey(tokenizationString[i]) ? hashMap.get(tokenizationString[i]) : 0;
hashMap.put(tokenizationString[i], count + 1);

首先,您的地图应该类似于
map
(字符串及其频率)。 我将为您提供Java8流解决方案

    public static void main(String[] args) {
    try (Stream<String> lines = Files.lines(Paths.get("out.txt"))) {
        Map<String, Long> frequency = lines
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (o, n) -> o,
                        LinkedHashMap::new
                ));

    } catch (IOException e) {
        e.printStackTrace();
    }
}
publicstaticvoidmain(字符串[]args){
try(streamlines=Files.lines(path.get(“out.txt”)){
地图频率=线
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(collector.toMap)(
Map.Entry::getKey,
Map.Entry::getValue,
(o,n)->o,
LinkedHashMap::新建
));
}捕获(IOE异常){
e、 printStackTrace();
}
}

上述代码将从文件中逐行读取。然后收集作为频率图。然后再次将它们转换为
入口集的流。然后根据值按相反顺序对流进行排序。最后,收集它们作为一个整体
LinkedHashMap
,因为它将维护插入顺序。看看Java8流API。

您可以通过Google的MultiMap类实现这一点,如下所示。也可在此链接中找到工作示例-

FileInputStream fstream=null;
BufferedReader br=null;
试一试{
fstream=newfileinputstream(“C:\\temp\\output.txt”);
br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
Multimap Multimap=ArrayListMultimap.create();
//逐行读取文件
而((strLine=br.readLine())!=null){
多重映射put(strLine,strLine);
}
for(字符串键:multimap.keySet()){
System.out.println(key+“被使用”+multimap.get(key.size()+“times”);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(fstream!=null){
fstream.close();
}
如果(br!=null){
br.close();
}
}

嗨,我已经试过了,但代码不起作用。你是用Java写的吗?我将上面的代码更改为Map hashMap=newhashmap();它是有效的。因此,hashmap的结构是String:是映射整数的键:是该键重复的次数。是的,您需要将String作为hashmap的键,将count作为值。
    public static void main(String[] args) {
    try (Stream<String> lines = Files.lines(Paths.get("out.txt"))) {
        Map<String, Long> frequency = lines
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (o, n) -> o,
                        LinkedHashMap::new
                ));

    } catch (IOException e) {
        e.printStackTrace();
    }
}
FileInputStream fstream = null;
    BufferedReader br = null;
    try {
        fstream = new FileInputStream("C:\\temp\\output.txt");
         br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;

        Multimap<String, String> multimap = ArrayListMultimap.create();
        // Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            multimap.put(strLine, strLine);
        }

        for (String key : multimap.keySet()) {
            System.out.println(key + "was used " + multimap.get(key).size() + "times");
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fstream != null) {
            fstream.close();
        }
        if(br!=null){
            br.close();
        }
    }