如何找到两个 java 列表之间的差异?

How to find the difference between two java list?

我有一个字符串 s1 = Hooray In this case you know, you care you want to emphasize, that we won the match 和一个字符串 s2 = Hooray ! In this case you know, you care you want to emphasize, that we won the match.

因为字符串 s1 和 s2 有两个不同 !.

我尝试了一种通用的方法来找出差异

     List<String> strings1 = Arrays.asList(s1.split("\s+"));
     List<String> strings2 = Arrays.asList(s2.split("\s+"));
     difference(strings2 ,strings1);
    public <T> List<T> difference(List<T> s2, List<T> s1) {
        List<T> toReturn = new ArrayList<>(s2);
        toReturn.removeAll(s1);
        return toReturn;
    }

根据我的逻辑,我在列表中得到的最终结果是 !match.,但我只想要 !. 确切的差异

有没有更好的方法来找出列表之间的差异。

好吧,您可以使用 org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch 来完成这项工作。只需将库导入您的项目并执行:

DiffMatchPatch dmp = new DiffMatchPatch();
LinkedList<DiffMatchPatch.Diff> diff = dmp.diffMain(s1, s2, false);
System.out.println(diff);

它将差异打印为 Diff 个对象,如下所示:

[Diff(EQUAL,"Hooray "), Diff(INSERT,"! "), Diff(EQUAL,"In this case you know, you care you want to emphasize, that we won the match"), Diff(INSERT,".")]

因此,要提取差异,您可以这样做:

for (DiffMatchPatch.Diff tokens: diff) {
    if (tokens.operation != DiffMatchPatch.Operation.EQUAL) {
        System.out.println(tokens.text);
    }
}

您不仅应该按空格 \s+ 拆分,还应该按单词边界拆分 \b:

String pattern = "\s+|\b";
List<String> strings1 = Arrays.asList(s1.split(pattern));
List<String> strings2 = Arrays.asList(s2.split(pattern));
System.out.println(strings2);
difference(strings2, strings1).forEach(System.out::println);

输出:

!
.