In my java program, i am getting result like
Output:
acre care race
act cat
Expected:
act cat
acre care race
Now I want to sort the Map elements in reverse order. So I can get the result like above. I added my code in below.
public static void main(String args[]) {
try {
Scanner sc = readWords();
Map<String, List<String>> wordAnagramPairs = new HashMap<>();
wordAnagramPairs = mapAnagrams(sc);
Comparator<List<String>> c = (l1, l2) -> {
Collections.sort(l1);
Collections.sort(l2);
int in = l1.get(0).length() - l2.get(0).length();
if (in == 0) {
return String.join(" ", l1).compareTo(String.join(" ", l2));
} else {
return in;
}
};
List<List<String>> sortedList = wordAnagramPairs.values()
.stream()
.filter(li -> li != null && li.size() > 1)
.sorted(c)
.collect(Collectors.toList());
for(List<String> anagrams : sortedList){
for(String anagram : anagrams){
System.out.print(anagram + " ");
}
System.out.print('\n');
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
}
TreeMapinstead ofHashMap