Count how many times a word occurs in a HashMap per key

OnlyProblems

I have a HashMap in which sentence records are stored with an associated key. Now, an output should be created in this displayed how much the word (in my example the word "car") in this index (key) occurs. For example, if the word "car" occurs in the index (key) 5, 4 times, the 5 should also be output 4 times.

The current output is: Car : [1, 2, 3, 5]

My desired output is: Car : [1, 1, 2, 3, 3, 5, 5, 5, 5]

Map<Integer, String> carHashMap = new HashMap<>();
ArrayList<Integer> showCarsInMap = new ArrayList<>();

carHashMap.put(1, "this car is very fast car");
carHashMap.put(2, "i have a old car");
carHashMap.put(3, "my first car was an mercedes and my second car was an audi");
carHashMap.put(4, "today is a good day");
carHashMap.put(5, "car car car car");

for (Map.Entry<Integer, String> entrySection : carHashMap.entrySet()) {

    if (entrySection.getValue().contains("car")) {

        showCarsInMap.add(entrySection.getKey());

    }
}

System.out.println("Car : " + showCarsInMap);
    

I guess I have to add an additional if-loop, but I don't know how my program can recognize which "car" has already been counted and which not.

Vinz

I suggest you simply use regex for this:

for (Map.Entry<Integer, String> entrySection : carHashMap.entrySet()) {
     Pattern p = Pattern.compile("(?:^|\\W)car(?:$|\\W)"); //This will only capture the exact word, as dicussed in the comments.
     Matcher m = p.matcher(entrySection.getValue());
     while (m.find()) {
           showCarsInMap.add(entrySection.getKey());
     }
}

Regex is from here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

XQuery: How to count how many times a value occurs in sequence

From Dev

Count how many times a value occurs across tables

From Dev

Perl: How to count number of times 3-word phrase (with gaps) occurs within an N-word window

From Dev

Count how many times specific element occurs in array

From Dev

How to count the number of times a value occurs in each column (per column), using Pandas?

From Dev

Count how many times a special character (non-spacing mark) occurs in a string in Swift

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

count how many times each item occurs in a dictionary

From Dev

Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

From Dev

Python: Count how many times a word occurs in a file

From Dev

Count how many times a word appears in a text file

From Dev

how to count how many times each word appears?

From Dev

How to count how many times a number occurs in each column of a file?

From Dev

How to count how many times a word is in the start and in the end?

From Dev

Count how many times a time occurs within a date range

From Dev

Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

From Dev

how to count how many times a string occurs in a column using pandas

From Dev

How to count how many times a user presses a key in a given time

From Dev

Count how many times a word exist in column

From Dev

How to count how many times a string occurs

From Dev

Is it possible to count or tally in a registry key how many times this button is pushed?

From Dev

Make a function that shows how many times a word occurs in a string/sentence

From Dev

Is there a way to count how many times a specific word appears in R

From Dev

How to replace a key:value pair by its value whereever the chosen key occurs in a many-times-nested dictionary?

From Dev

Count how many times a certain value per user has changed

From Dev

I need to check if word in list of word exist in list of strings and how many times it occurs and return dict({word: total_occurrence})

From Dev

Count how many times a value occurs in a range (based on the value in another column)

From Dev

Count how many times a value appears per month in dataframe

From Dev

Numpy, How to count how many times a string occurs

Related Related

  1. 1

    XQuery: How to count how many times a value occurs in sequence

  2. 2

    Count how many times a value occurs across tables

  3. 3

    Perl: How to count number of times 3-word phrase (with gaps) occurs within an N-word window

  4. 4

    Count how many times specific element occurs in array

  5. 5

    How to count the number of times a value occurs in each column (per column), using Pandas?

  6. 6

    Count how many times a special character (non-spacing mark) occurs in a string in Swift

  7. 7

    Count how many times certain text combinations occurs in certain columns

  8. 8

    count how many times each item occurs in a dictionary

  9. 9

    Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

  10. 10

    Python: Count how many times a word occurs in a file

  11. 11

    Count how many times a word appears in a text file

  12. 12

    how to count how many times each word appears?

  13. 13

    How to count how many times a number occurs in each column of a file?

  14. 14

    How to count how many times a word is in the start and in the end?

  15. 15

    Count how many times a time occurs within a date range

  16. 16

    Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

  17. 17

    how to count how many times a string occurs in a column using pandas

  18. 18

    How to count how many times a user presses a key in a given time

  19. 19

    Count how many times a word exist in column

  20. 20

    How to count how many times a string occurs

  21. 21

    Is it possible to count or tally in a registry key how many times this button is pushed?

  22. 22

    Make a function that shows how many times a word occurs in a string/sentence

  23. 23

    Is there a way to count how many times a specific word appears in R

  24. 24

    How to replace a key:value pair by its value whereever the chosen key occurs in a many-times-nested dictionary?

  25. 25

    Count how many times a certain value per user has changed

  26. 26

    I need to check if word in list of word exist in list of strings and how many times it occurs and return dict({word: total_occurrence})

  27. 27

    Count how many times a value occurs in a range (based on the value in another column)

  28. 28

    Count how many times a value appears per month in dataframe

  29. 29

    Numpy, How to count how many times a string occurs

HotTag

Archive