Class HammingDistance
- java.lang.Object
-
- org.apache.commons.text.similarity.HammingDistance
-
- All Implemented Interfaces:
java.util.function.BiFunction<java.lang.CharSequence,java.lang.CharSequence,java.lang.Integer>,EditDistance<java.lang.Integer>,ObjectSimilarityScore<java.lang.CharSequence,java.lang.Integer>,SimilarityScore<java.lang.Integer>
public class HammingDistance extends java.lang.Object implements EditDistance<java.lang.Integer>
The hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.For further explanation about the Hamming Distance, take a look at its Wikipedia page at https://en.wikipedia.org/wiki/Hamming_distance.
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description HammingDistance()Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Integerapply(java.lang.CharSequence left, java.lang.CharSequence right)Computes the Hamming Distance between two strings with the same length.<E> java.lang.Integerapply(SimilarityInput<E> left, SimilarityInput<E> right)Computes the Hamming Distance between two strings with the same length.
-
-
-
Constructor Detail
-
HammingDistance
public HammingDistance()
Creates a new instance.
-
-
Method Detail
-
apply
public java.lang.Integer apply(java.lang.CharSequence left, java.lang.CharSequence right)
Computes the Hamming Distance between two strings with the same length.The distance starts with zero, and for each occurrence of a different character in either String, it increments the distance by 1, and finally return its value.
Since the Hamming Distance can only be calculated between strings of equal length, input of different lengths will throw IllegalArgumentException
distance.apply("", "") = 0 distance.apply("pappa", "pappa") = 0 distance.apply("1011101", "1011111") = 1 distance.apply("ATCG", "ACCC") = 2 distance.apply("karolin", "kerstin" = 3- Specified by:
applyin interfacejava.util.function.BiFunction<java.lang.CharSequence,java.lang.CharSequence,java.lang.Integer>- Specified by:
applyin interfaceObjectSimilarityScore<java.lang.CharSequence,java.lang.Integer>- Specified by:
applyin interfaceSimilarityScore<java.lang.Integer>- Parameters:
left- the first input, must not be null.right- the second input, must not be null.- Returns:
- distance.
- Throws:
java.lang.IllegalArgumentException- if either input isnullor if they do not have the same length.
-
apply
public <E> java.lang.Integer apply(SimilarityInput<E> left, SimilarityInput<E> right)
Computes the Hamming Distance between two strings with the same length.The distance starts with zero, and for each occurrence of a different character in either String, it increments the distance by 1, and finally return its value.
Since the Hamming Distance can only be calculated between strings of equal length, input of different lengths will throw IllegalArgumentException
distance.apply("", "") = 0 distance.apply("pappa", "pappa") = 0 distance.apply("1011101", "1011111") = 1 distance.apply("ATCG", "ACCC") = 2 distance.apply("karolin", "kerstin" = 3- Type Parameters:
E- The type of similarity score unit.- Parameters:
left- the first input, must not be null.right- the second input, must not be null.- Returns:
- distance.
- Throws:
java.lang.IllegalArgumentException- if either input isnullor if they do not have the same length.- Since:
- 1.13.0
-
-