0

I have used Java Comparator to sort an ArrayList of Word objects in descending order of Word frequency attribute. The Word objects are created by first using a hashmap to read the words from a .txt file and then convert the hashmap to an ArrayList of Word objects. I would like to then sort the words that have the same frequency by alphabetical order.

   while (reader.hasNext()) {
            word = reader.next().toLowerCase();
            word = word.substring(0, 1).toUpperCase() + word.substring(1);
            word = word.replaceAll("[^a-zA-Z ]", "");
            if (!word.contains("0") || !word.contains("1") || !word.contains("2") || !word.contains("3") || !word.contains("4") || !word.contains("5") || !word.contains("6") || !word.contains("7") || !word.contains("8") || !word.contains("9") || !word.contains("-") || !word.contains("_")) {
                // This is equivalent to searching every word in the list via hashing (O(1))
                if(!frequencyMap.containsKey(word)) {
                     frequencyMap.put(word, 1);
                } else {
                        // We have already seen the word, increase frequency.
                        frequencyMap.put(word, frequencyMap.get(word) + 1);
                }
            }
            counter++;
        }

 for(Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
    Word word = new Word(entry.getKey());
    word.frequency = entry.getValue();
    wordList.add(word);
 }
 Collections.sort(wordList, Word.WordFrequency);

public class Word {

    String value;
    int frequency;

    public Word(String v) {
        value = v;
        frequency = 1;
    }

    public String getValue() {
        return value;
    }

    public int getFrequency() {
        return frequency;
    }

    public static Comparator<Word> WordFrequency = new Comparator<Word>() {
        public int compare(Word w1, Word w2) {
            int w1Frequency = w1.getFrequency();
            int w2Frequency = w2.getFrequency();
            return w2Frequency-w1Frequency;
        }
    };
}
3
  • 1
    if (w2Frequency - w1Frequency == 0) { return /* compareTheStrings*/; } Commented Nov 26, 2018 at 18:53
  • 1
    Possible duplicate of Java stream sort 2 variables ascending/desending Commented Nov 26, 2018 at 18:54
  • @luk2302 Thanks for the help, if you want to post an answer here's what I went with: if (w1Frequency == w2Frequency) { return w1.getValue().compareTo(w2.getValue()); } else { return w2Frequency-w1Frequency; } Commented Nov 26, 2018 at 19:09

1 Answer 1

2

See the thenComparing method, which allows you to supply a comparison key for when there are ties:

// sort using 'd' will sort 1st alphabetically, then by length
// (this is a totally arbitrary example)
Comparator<String> c = String::compareTo;
Comparator<String> d = c.thenComparing(s -> s.length());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.