Java 8 - How to find the closest name in a list

Viewed 78

I am using java 8 and I am trying to implement an algorithm to try to find the correct name in a list or else, the closest name.

For example :

**Name**  
Smith Johnson Williams Brown

**Lists of names**
Johnson 
Johnson Williams Brown
Smith Johnson Brown Williams
Smith Johnson
Smith Williams Brown Johnson

In this case, the algorithm must return "Smith Johnson Brown Williams" because of the order of the strings.

Of course if we add "Smith Johnson Williams Brown" to the list of names, it will return only this name, but if it's not present, we have to return the closest name regarding the order.

Can any one help on this please?

Thank you.

1 Answers

Levenshtein algorithm allows you to compare the similarity between two strings, returning an integer. Levenshtein counts the number of edits (insertions, deletions, or substitutions) needed to convert one string to the other.

Another algorithm which can be usefull is Jaro-Winkler algorithm.

More details : Difference between Jaro-Winkler and Levenshtein distance?

You can also find some implementations of these algorithms on internet in java.

Related