Вот пример хорошего вывода:
Код: Выделить всё
лицами-18
Apex-15
azet-15
xder-15
анатолю-15
андреевич-15
батальона-15
hello-13
zello-13
полноте-13
Код: Выделить всё
public class Words {
public String countWords(List lines) {
StringBuilder input = new StringBuilder();
StringBuilder answer = new StringBuilder();
for (String line : lines){
if(line.length() > 3){
if(line.substring(line.length() - 1).matches("[.?!,]+")){
input.append(line.substring(0,line.length()-1)).append(" ");
}else{
input.append(line).append(" ");
}
}
}
String[] strings = input.toString().split("\\s");
List list = new ArrayList(Arrays.asList(strings));
Map unsortMap = new HashMap();
while (list.size() != 0){
String word = list.get(0);
int freq = Collections.frequency(list, word);
if (word.length() >= 4 && freq >= 10){
unsortMap.put(word.toLowerCase(), freq);
}
list.removeAll(Collections.singleton(word));
}
//The Stream logic is here
List sortedEntries = unsortMap.entrySet().stream()
.sorted(Comparator.comparingLong(Map.Entry::getValue)
.reversed()
.thenComparing(Map.Entry::getKey)
)
.map(it -> it.getKey() + " - " + it.getValue())
.collect(Collectors.toList());
//Logic ends here
for (int i = 0; i < sortedEntries.size(); i++) {
if(i
Подробнее здесь: [url]https://stackoverflow.com/questions/68662616/alternative-of-streams-in-sorting-frequently-occurred-words[/url]
Мобильная версия