
uk.co.mruoc.string.firstunique.FirstUniqueCharacter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of string-utils Show documentation
Show all versions of string-utils Show documentation
Library containing string utilites
The newest version!
package uk.co.mruoc.string.firstunique;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
public class FirstUniqueCharacter {
private FirstUniqueCharacter() {
// utility class
}
public static char forceFind(String input) {
return find(input).orElseThrow(() -> new NoUniqueCharactersException(input));
}
public static Optional find(String input) {
validate(input);
return toUniqueChars(input).findFirst();
}
private static void validate(String input) {
if (input == null) {
throw new NoUniqueCharactersException("cannot pass null string");
}
}
private static Stream toUniqueChars(String input) {
Set uniqueChars = new LinkedHashSet<>();
for (char c : input.toCharArray()) {
if (uniqueChars.contains(c)) {
uniqueChars.remove(c);
} else {
uniqueChars.add(c);
}
}
return uniqueChars.stream();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy