Reputation: 29
Removing brackets from list of anagrams
Hello, i have been asked to create new question insted of asking again in the same thread.
public static void main(String[] args) throws IOException {
getAnagrams(new InputStreamReader(new URL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").openStream(),
StandardCharsets.UTF_8)).forEach(items -> {
for (String item : items) {
System.out.print(item + " ");
}
System.out.println();
});
}
private static String canonicalize(String string) {
return Stream.of(string.split("")).sorted().collect(Collectors.joining());
}
public static List<Set<String>> getAnagrams(Reader rr) {
Map<String, Set<String>> mapa = (Map<String, Set<String>>) new BufferedReader(rr).lines()
.flatMap(Pattern.compile("\\W+")::splitAsStream)
.collect(Collectors.groupingBy(Main::canonicalize, Collectors.toSet()));
return mapa.values().stream().filter(lista -> lista.size() > 4).collect(Collectors.toList());
}
}
How do i sort my output like "evil levi live veil vile"? Cuz for now i have "veil evil vile levi live"
UPDATE Last thing is to sort output alphabeticaly
For now = "evil levi live veil vile, abel able bale bela elba"
Needed = "abel able bale bela elba, evil levi live veil vile"
Upvotes: 0
Views: 92
Reputation: 12513
Here's the easiest way I can think of
return mapa.values().stream()
.filter(lista -> lista.size() > 4)
.map(set -> new TreeSet<>(set))
.collect(Collectors.toList());
TreeSet
keeps its elements sorted.
Upvotes: 2