Top Description Constructors Methods
org.apache.commons.text.similarity

public Class HammingDistance

extends Object
implements EditDistance<Integer>
Class Inheritance
All Implemented Interfaces
org.apache.commons.text.similarity.EditDistance, org.apache.commons.text.similarity.SimilarityScore, org.apache.commons.text.similarity.ObjectSimilarityScore, java.util.function.BiFunction

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

AccessConstructor and Description
public
HammingDistance()

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public Integer

Returns:

distance.
apply
(final CharSequence
the first input, must not be null.
left
,
final CharSequence
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

Computes the Hamming Distance between two strings with the same length.

public <
The type of similarity score unit.
E
>
Integer

Returns:

distance.
apply
(final SimilarityInput<E>
the first input, must not be null.
left
,
final SimilarityInput<E>
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

Computes the Hamming Distance between two strings with the same length.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

HammingDistanceback to summary
public HammingDistance()

Creates a new instance.

Method Detail

applyback to summary
public Integer apply(final CharSequence left, final CharSequence right)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

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
Parameters
left:CharSequence

the first input, must not be null.

right:CharSequence

the second input, must not be null.

Returns:Integer

distance.

Annotations
@Override
Exceptions
IllegalArgumentException:
if either input is null or if they do not have the same length.
applyback to summary
public <E> Integer apply(final SimilarityInput<E> left, final SimilarityInput<E> right)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

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
Parameters
<E>
The type of similarity score unit.
left:SimilarityInput<E>

the first input, must not be null.

right:SimilarityInput<E>

the second input, must not be null.

Returns:Integer

distance.

Exceptions
IllegalArgumentException:
if either input is null or if they do not have the same length.
Since
1.13.0