接受一个字符串和一个字符串列表作为参数的 Java 方法,用于计算两个字符串之间的相似度。
方法
import java.util.HashSet;
import java.util.List;
import java.util.Set;public class StringSimilarity {public static String findMostSimilarString(String str, List<String> list1) {Set<String> set1 = new HashSet<>(list1);set1.remove(str);long intersectionSize = set1.stream().filter(s -> s.equals(str)).count();long strLength = str.length();long unionSize = set1.size() + strLength - intersectionSize;double similarity = (double) intersectionSize / unionSize;String mostSimilarString = null;double maxSimilarity = Double.MIN_VALUE;for (String s : list1) {double currentSimilarity = calculateSimilarity(str, s, list1);if (currentSimilarity > maxSimilarity) {maxSimilarity = currentSimilarity;mostSimilarString = s;}}return mostSimilarString;}private static double calculateSimilarity(String str1, String str2, List<String> list2) {Set<String> set2 = new HashSet<>(list2);set2.remove(str2);long intersectionSize = set2.stream().filter(s -> s.equals(str2)).count();long strLength = str1.length();long unionSize = set2.size() + strLength - intersectionSize;return (double) intersectionSize / unionSize;}
}
使用示例
List<String> list1 = Arrays.asList("apple", "banana", "orange");
String str = "banana";
String mostSimilarString = StringSimilarity.findMostSimilarString(str, list1);
System.out.println("Most similar string: " + mostSimilarString);