/**
* Finds a substring of value that uniquely identifies it from all strings
* in a collection.
*
* @param value the target string
* @param collection the collection of other strings
* @return a unique substring of value, or value itself if no such substring
* exists
*/
public static String findUniqueSubstring(final String value,
final Collection<String> collection) {
return IntStream.range(1, value.length() + 1)
.mapToObj(len -> IntStream.range(0, value.length() - len + 1)
.mapToObj(begin -> value.substring(begin, begin + len))
.filter(substring -> collection.parallelStream()
.noneMatch(other -> other.contains(substring)))
.findFirst())
.filter(Optional::isPresent).map(Optional::get).findFirst()
.orElse(value);
}
I’m getting a runtime “java.lang.ClassCastException: class org.eclipse.parsson.JsonStringImpl cannot be cast to class java.lang.String” in the “noneMatch” line.
If it’s relevant: It’s in a JavaFX application, using jakarta.json and its parsson implementation.