Java 将 List 和 Map 中的值相乘

标签 java list dictionary

我有一个代码可以从列表和 map 中输出数据。如何将相应的数字相乘?

列表的输出

{prepare=0.25, saturday=0.5, republic=0.25, approach=0.25, ...}

map 的输出

[0, 1, 0, 0,...]

我需要将它们与相应的数字相乘并求和。 总计将是我想要的输出,如下所示:

(0.25*0)+(0.5*1)+(0.25*0)+(0.25*0).... = total 

这是我的示例代码:

import java.util.*;
import java.util.stream.Collectors;

class WordOcc {
  public static void main(String[] args) {
    String someText = "hurricane gilbert head dominican coast saturday. hurricane gilbert sweep dominican republic sunday civil  prepare high wind heavy rain high sea. storm approach southeast sustain wind  mph   mph. there alarm civil defense director   a television alert shortly midnight saturday.";

    List<List<String>> sort = new ArrayList<>();
    Map<String, ArrayList<Integer>> res = new HashMap<>();

    for (String sentence : someText.split("[.?!]\\s*"))
    {
        sort.add(Arrays.asList(sentence.split("[ ,;:]+"))); //put each sentences in list
    }

    // put all word in a hashmap with 0 count initialized
    int sentenceCount = sort.size();

    for (List<String> sentence: sort) {
        sentence.stream().forEach(x -> res.put(x, new ArrayList<Integer>(Collections.nCopies(sentenceCount, 0))));
    }

    int index = 0;
    // count the occurrences of each word for each sentence.
    for (List<String> sentence: sort) {
        for (String q : sentence) {
            res.get(q).set(index, res.get(q).get(index) + 1); 
        }
        index++;
    }


    Map<String,Float>word0List = getFrequency(res);

    System.out.println(word0List + "\n"); //print the list value      

    //get first sentence count value from each word
    List<Integer> sentence0List = getSentence(0, res);

    System.out.println(sentence0List + "\n"); //print the map value
  }

  static List<Integer> getSentence(int sentence, Map<String, ArrayList<Integer>> map) {
      return map.entrySet().stream().map(e -> e.getValue().get(sentence)).collect(Collectors.toList());
   }

 static Map<String, Float> getFrequency(Map<String, ArrayList<Integer>> stringMap) {
  Map<String, Float> res = new HashMap<>();
 stringMap.entrySet().stream().forEach(e -> res.put(e.getKey(), e.getValue().stream().mapToInt(Integer::intValue).sum() / (float)e.getValue().size()));
        return res;
      }

}

感谢对此的任何帮助。谢谢!

最佳答案

我不确定这是否是您正在寻找的内容,因为正如评论中提到的那样, HashMap 没有预期的顺序。但正如您所描述的,您想做这样的事情:

float result = 0;
Float[] hmValues = word0List.values().toArray(new Float[0]);
for(int i = 0; i < word0List.size(); i++)
    result += hmValues[i] * sentence0List.get(i);

关于Java 将 List 和 Map 中的值相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993672/

相关文章:

python - 列出字典中以 <user input> 开头的所有单词

python - 字典理解没有按预期进行

java - 即使应用程序在 java/javafx 中关闭,它也会在后台运行

java - 是否可以在 Java 中对 float 执行位操作?

java - 如何使用 Apache ISI 中的 jdo 查询从表行号在 20 到 30 之间获取记录

python - 如何从Python列表中计算数学问题?

java - 在 JMS 中一次处理来自 IBM MQ 的消息

java - Iterator和Listiterator的迭代器规则有什么不同吗?(Java)

python - 按星期几对数据进行分类 Python

c# - LINQ 根据列表查询字典