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:


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?
Wordclass? And why did you create aMap<Integer, Word>, and not aMap<String, Integer> as in the example? Calling a variable of typeMapaslistis a bad decision. - RegentWordis superfluous here, since the pairs “word - quantity” are stored in you (and in Hortsmann) as aMap, and not as an add. class. The question is, what do you want to achieve? You, for example, also haveRandomin 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