-2

I have got an ArrayList with 500+ words in. I'm trying to organise them into a list where the word that appears the most is at the top and then the 2nd most frequent and so on.

So far I've managed to filter out words with a frequency of less than 5 with the code below however I cannot work out how I can organise these results into a list of their frequencies in descending order.

    Set<String> unique = new HashSet<String>(wordsL);

    for (String key : unique) {
      if (Collections.frequency(wordsL, key) > 5) {
//        println(Collections.frequency(wordsL, key));
        lwords.add(key);
        println(lwords);
      }
    }

Thanks in advance for any help.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
John Smith
  • 276
  • 1
  • 2
  • 15

1 Answers1

1

You can use Map<String, Integer> for counting.

You can later sort the map by value.

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61