Visit the isogram exercise on Exercism to read the full instructions and download the exercise files.
Dig Deeper
filter(), map() and allMatch()
filter() and map() with allMatch()
import java.util.HashSet;
final class IsogramChecker {
boolean isIsogram(final String phrase) {
return phrase.chars()
.filter(Character::isLetter)
.map(Character::toLowerCase)
.allMatch(new HashSet < > ()::add);
}
}
This approach starts by importing HashSet.
In the isIsogram() method, the chars() method is called on the input String.
Each character is passed as a primitive int representing its Unicode codepoint to the filter() method.
The filter() passes each codepoint to the IsLetter() method to filter in only letter characters.
Another method that could be used is [`isAlphabetic()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-).
For the difference between `isAlphabetic()` and `isLetter()`, see [here](https://www.baeldung.com/java-character-isletter-isalphabetic).
The surviving codepoints are passed to the map() method which converts the codepoints to lower case.
The lowercased codepoints are passed into the allMatch() method which passes each codepoint
into the HashSet’s add() method.
The add() method returns true if the value is not already in the HashSet and false if the value is already in the HashSet.
If add() returns false, then the allMatch() method can know that it will never be true and short-circuit
with a result of false.
If add() always returns true, then allMatch() will return true.
Finally, the result of allMatch() is returned from the IsIsogram() function.
filter(), mapToObj() and distinct()
filter() and mapToObj() with distinct()
import java.util.stream.Collectors;
public class IsogramChecker {
public boolean isIsogram(String input) {
final var scrubbed = input.chars()
.filter(Character::isLetter)
.mapToObj(Character::toLowerCase)
.collect(Collectors.toList());
return scrubbed.size() == scrubbed.stream().distinct().count();
}
}
This approach starts by importing Collectors.
In the isIsogram() method, the chars() method is called on the input String.
Each character is passed as a primitive int representing its Unicode codepoint to the filter() method.
The filter() passes each codepoint to the IsLetter() method to filter in only letter characters.
Another method that could be used is [`isAlphabetic()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-).
For the difference between `isAlphabetic()` and `isLetter()`, see [here](https://www.baeldung.com/java-character-isletter-isalphabetic).
The surviving codepoints are passed to the mapToObj() method which converts each codepoint to a lowercased codepoint.
The collect() method assembles the lowercased codepoints into a List of codepoint Integers.
The size() of the List is compared with the count() of the distinct() letters in the List.
If they are equal, then there are no duplicate letters and the comparison returns true from the isIsogram() method.
If they are not equal, then there are one or more duplicate letters and the comparison returns false from the isIsogram() method.
Lihat Solusi Komunitas
The following are the top 3 community solutions by stars:
Source: Exercism java/isogram