Search code examples
javadictionaryhashinverted-index

Count number of values of a list having same hash value in java?


I want to count number of values having same hash value. invertedindex element {{I1, I2},"book"}.

//mycode

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

it prints 0 always. what should be in ??? place


Solution

  • Don't know if I understand exactly what u want but if u want to find in that map how many records have a value u should use it like this :

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

    This will print 2.