HammingDistance (Apache Commons Text 1.11.0 API)
Description: declaration: package: org.apache.commons.text.similarity, class: HammingDistance

Package org.apache.commons.text.similarity

# Class HammingDistance

java.lang.Object
org.apache.commons.text.similarity.HammingDistance

All Implemented Interfaces:
  `EditDistance<Integer>`, `SimilarityScore<Integer>`

public class HammingDistance extends Object implements EditDistance<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 http://en.wikipedia.org/wiki/Hamming_distance.

Since:
  1.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `HammingDistance()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Integer`
  `apply(CharSequence left, CharSequence right)`
  Find the Hamming Distance between two strings with the same length.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### HammingDistance

    public HammingDistance()

- ## Method Details

  - ### apply

    public Integer apply(CharSequence left, CharSequence right)
    Find 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:
      `apply` in interface `EditDistance<Integer>`
    Specified by:
      `apply` in interface `SimilarityScore<Integer>`
    Parameters:
      `left` - the first CharSequence, must not be null
      `right` - the second CharSequence, must not be null
    Returns:
      distance
    Throws:
      `IllegalArgumentException` - if either input is `null` or if they do not have the same length