Search code examples
javastringdictionarysortingcollections

Count word with a method where it's input parameter is a list of strings representing lines of text


I have to write an implementation of String method, where it's input parameter is a list of strings representing lines of text - Count words in a text and return statistics.

  1. How can I split the String with spaces, so it will also ignore special characters?
  2. How can I sort the map first by it's numerical order of values and then by it's alphabetical order of keys. Here are the restrictions/hints I have: "The input parameter is a list of strings representing lines of text.

Count how often the word occurs in the text. If the word "kitten" occurred in a text 23 times then its entry would be "kitten - 23\n". Return statistics as a String containing all the entries.

Omit all words which contain less than 4 letters and appear less less than 10 (the words which are too small or to rare) The entries in the resulting String should be also sorted by their amount and then in alphabetical order if it is needed. Be sure to make your code handling texts that are not in English.

You may not use streams, lambdas or method references in your code"

`public class Words {

public String countWords(List<String> lines) {
    String result = "";
    HashMap<String, Integer> wordMap = mapOfWords(lines);
    for (String key : wordMap.keySet()) {
        if (wordMap.get(key) > 10) {
            result += key + " - " + wordMap.get(key);
            result+= "\n";
        }
    }
    return result;
}

private HashMap<String, Integer> mapOfWords(List<String> lines) {
    HashMap<String, Integer> map = new HashMap<>();
    for (String line : lines) {
        String[] wordList = line.split("[ ,.-?]");
        for (String word: wordList) {
            if (word.length() >= 4) {
                if (map.containsKey(word)) {
                    map.put(word, map.get(word) + 1);
                } else {
                    map.put(word, 1);
                }
            }
        }
    }
    return map;
}

}`

I have written code that counts word occurrence frequency and used map to store words as key and it's occurrence frequency as value. There are restrictions for using streams, lambdas or method references.


Solution

  • for the sorting:

    • Sorting by values

       Map<String, Integer> unsortedMap = new HashMap<>();
       unsortedMap.put("John", 30);
       unsortedMap.put("Alice", 25);
       unsortedMap.put("Bob", 35);
       unsortedMap.put("Eva", 28);
      
       // Convert the map entries to a list
       List<Map.Entry<String, Integer>> entryList = new ArrayList<>(unsortedMap.entrySet());
      
       // Sort the list based on the values
       Collections.sort(entryList, Comparator.comparing(Map.Entry::getValue));
      
       // Create a new LinkedHashMap to preserve the order
       Map<String, Integer> sortedMap = new LinkedHashMap<>();
       for (Map.Entry<String, Integer> entry : entryList) {
           sortedMap.put(entry.getKey(), entry.getValue());
       }
      
    • by keys Just use TreeMap instead of HashMap

    for splitting String may I suggest using split(" ") example:

    String[] splittedStr = str.split(" ");
    splittedStr[0] => "Hello"
    splittedStr[1] => "World"```