【问题标题】:Count number of values of a list having same hash value in java?在java中计算具有相同哈希值的列表的值的数量?
【发布时间】:2018-03-12 14:09:10
【问题描述】:

我想计算具有相同哈希值的值的数量。 倒排索引元素 {{I1, I2},"book"}。

//我的代码

for (String s1 : uniquewordslist) 

{

   if (invertedindex.containsKey(s1))

 {

    List<String> l1 = new ArrayList<>();

    l1 = invertedindex.get(s1);
    if (!l1.contains(key)) {
    l1.add(key);

    }
    invertedindex.put(s1, l1);
} 
else {
     List<String> l2 = new ArrayList<String>();

     l2.add(key);

     invertedindex.put(s1, l2);


      }

   int count = Collections.frequency( ?????, invertedindex.values()); // 

   System.out.println(count); 

它总是打印 0。应该是什么???地点

【问题讨论】:

  • 您正在评估的哈希值在哪里,为什么 invertedindex.get(i).size() 无法为您获取字符串键的列表值的大小?
  • myString==book 请考虑一下
  • 我没有得到 count_ 值。它应该打印 1。但它打印 0。
  • 为什么要打印1?请澄清,你想计算什么?您有一个倒排索引,因此该元素看起来像"book", ["string1", "string2", ..., "stringN"]。现在,你想数什么?
  • invertedindex.values() 返回一个包含所有列表的集合 - 这些列表未扁平化为单个列表Collections.frequency 计算此列表集合中出现的次数,但myString 不是列表而是单个元素。因此,它将是 0

标签: java dictionary hash inverted-index


【解决方案1】:

不知道我是否完全理解你想要什么,但如果你想在该地图中找到有多少记录具有值,你应该像这样使用它:

public static void main(String[] args) {
    Map<String, List<String>> invertedindex = new HashMap<String, List<String>>();
    List<String> myList = new ArrayList<>();
    myList.add("firstitem");
    myList.add("seconditem");
    invertedindex.put("book", myList);
    invertedindex.put("book2", myList);
    int count_ = Collections.frequency(invertedindex.values(), myList);
    System.out.println(count_);
}

这将打印 2。

【讨论】:

  • `for (String s1 : uniquewordslist) { if (invertedindex.containsKey(s1)) { List l1 = new ArrayList(); l1 = 倒排索引.get(s1); if (!l1.contains(key)) { l1.add(key); } 倒排索引.put(s1, l1); } else { List l2 = new ArrayList(); l2.add(key);倒排索引.put(s1, l2); } int count = Collections.frequency( ????? reverseindex.values()); System.out.println(count);'例如,倒排索引元素 {{I1, I2},"book"}。所以 count 将返回 2
猜你喜欢
  • 2011-03-10
  • 2016-10-08
  • 1970-01-01
  • 2019-04-23
  • 2013-08-05
  • 1970-01-01
  • 2011-10-04
  • 2015-04-19
  • 2020-04-05
相关资源
最近更新 更多