RosettaCodeData/Task/Ordered-words/Java/ordered-words-2.java

26 lines
863 B
Java
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public final class OrderedWords {
2026-02-01 16:33:20 -08:00
public static void main(String[] aArgs) throws IOException {
List<String> ordered = Files.lines(Path.of("unixdict.txt"))
.filter( word -> isOrdered(word) ).toList();
final int maxLength = ordered.stream().map( word -> word.length() ).max(Integer::compare).get();
ordered.stream().filter( word -> word.length() == maxLength ).forEach(System.out::println);
}
private static boolean isOrdered(String aWord) {
return aWord.chars()
.mapToObj( i -> (char) i )
.sorted()
.map(String::valueOf)
.reduce("", String::concat)
.equals(aWord);
}
2023-07-01 11:58:00 -04:00
}