Before me is the task to calculate the frequency of occurrence of words in the entered text. To store words, I use the TreeMap collection (for automatic sorting).
When searching for a solution to the problem, the Kay Hortsmann’s Java SE 8 directory was flipped through:

enter image description hereenter image description here

I'm interested in the line counts.put(word, counts.getOrDefault(word, 0) + 1); .

I tried to interpret it for my example, but nothing came of it. I do not have a full understanding of what is happening, and because all I can at this stage is to pull pieces of code from different sources and try to connect them together.

Here is my code:

 package firstPackage; import java.io.*; import java.util.*; public class Test { public static void main(String[] args) { Integer randomNumber; Console cons = System.console(); Map<Integer,Word> list = new TreeMap<>(); Random generator = new Random(); String myText = cons.readLine(); for (String word : myText.split(" ")) { randomNumber = generator.nextInt(100001); list.put(list.getOrDefault(0, word) + 1, word); } } class Word { Integer count = 0; String word; Word(Integer count, String word) { this.count = count; this.word = word; } } } 

In this case, Eclipse in the list.put(list.getOrDefault(0, word) + 1, word); line list.put(list.getOrDefault(0, word) + 1, word); writes an error:

The method of getOrDefault (Object, Word) in the type of map (int, String).

I do not understand what I have to do to fix it.

How to write code list.put(list.getOrDefault(0, word) + 1, word); so that it is suitable for use in my program? Where do I make a mistake?

  • Why did you create the Word class? And why did you create a Map<Integer, Word> , and not a Map<String, Integer > as in the example? Calling a variable of type Map as list is a bad decision. - Regent
  • @Regent Created a Word class to store the word and count in it, indicating how many times it has been encountered. Hortsmann does not give context to that line of code, and therefore I do not understand what he means. I do not understand where he has the key, where is the word, and where is the counter. In the Map you need to attach a key and value, right? The key can not show how many times the word has been met, so the number should be stored in the value. I reasoned in this way. Do you think the Word class is not needed? - Alexey Fedotov
  • Yes, Word is superfluous here, since the pairs “word - quantity” are stored in you (and in Hortsmann) as a Map , and not as an add. class. The question is, what do you want to achieve? You, for example, also have Random in the code for some reason. Do you just want to count the number of words from the string from the console or something else? - Regent
  • The Word class is not needed, the word should be stored in Map <String, Integer>, and the number corresponding to this word should also be stored there. - Leonid
  • @Regent I want to calculate the frequency with which each word occurs. Using Random, I create keys. Mr. Hortsmann in all examples writes them by hand. - Alexey Fedotov

1 answer 1

You can count the number of occurrences of each word from the string entered from the console as follows:

 Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); Map<String, Integer> wordToCount = new TreeMap<>(); for (String word : text.split(" ")) { wordToCount.put(word, wordToCount.getOrDefault(word, 0) + 1); } System.out.println(wordToCount); 

wordToCount (you have a list , Hortsmann's counts ) is a TreeMap<String, Integer> . That is, the key is the String (the word itself), and the value is the Integer (the number of occurrences of the word).

wordToCount.getOrDefault(word, 0) wordToCount number of occurrences of the word word stored in wordToCount , or 0 if the word word in wordToCount is not yet present.
After that, a unit is added to the resulting value (number) and the result is written to wordToCount with the word key.

This line can be written like this:

 int oldCount = wordToCount.getOrDefault(word, 0); int newCount = oldCount + 1; wordToCount.put(word, newCount);