簡體   English   中英

檢查list2.containsAll(list1)但字符串不完全相同

[英]check if list2.containsAll(list1) but with not exact same String

我有List1和List2,如果List1中的所有字符串都以類似的形式存在(請參見下文),我想收到true。

現在的問題是

List1:
1. iscat
2. ishooman
3. isdoge

List2:
1. is_Cat
2. is_Hooman
3. is_doge

從邏輯上講,函數list2.containsAll(list1)返回false,因為字符串不相等。

我如何檢查類似的字符串? 我可以想到Regex,但是現在我沒有一個明確的術語,我也不知道如何在Java中使用Regex。

謝謝

commons-collections4有一個CollectionUtils.isEqualCollection()方法,並以Equator(類似於equals()方法)作為輸入。

您可以生成一個赤道使這些字符串相同,然后調用isEqualCollection()

    Equator<String> equator = new Equator<String>() {
        @Override
        public boolean equate(String o1, String o2) {
            if  (StringUtils.equalsIgnoreCase(o1.replaceAll("_", ""), o2.replaceAll("_", ""))) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public int hash(String o) {
            return HashCodeBuilder.reflectionHashCode(o.replaceAll("_", "")).toLowerCase();
        }
    };
    List<String> a = new ArrayList<>();
    List<String> b = new ArrayList<>();
    a.add("iscat");
    b.add("is_Cat");
    System.out.println(CollectionUtils.isEqualCollection(a, b, equator));

並且有一個類似的CollectionUtils.removeAll()函數。 我只看到您只需要containAll(),因此可以改用removeAll。 如果在A removeAll B之后沒有任何剩余,我們可以看到B containsAllA。

GitHub上有一個可以檢查String相似性的API,您可以使用它!

可以看出Jaro-Winkler實現了相似度和距離(?)的算法。 檢查以下示例:

import info.debatty.java.stringsimilarity.*;

public class MyApp {


    public static void main(String[] args) {
        JaroWinkler jw = new JaroWinkler();

        // substitution of s and t
        System.out.println(jw.similarity("My string", "My tsring"));

        // substitution of s and n
        System.out.println(jw.similarity("My string", "My ntrisg"));
    }
}

輸出:

0.9740740656852722

0.8962963223457336

您可以在列表中進行迭代,調用此庫,然后保存結果以供以后比較

Java的字符串相似

好了,您可以檢查每個字符串是否包含另一個列表中的字符串中的所有字母(單向包含):

for(int i = 0; i<list1.size();i++){
    if(Collections.contains(list1.get(i).toLowerCase().toCharacterArray(),list2.get(i).toLowerCase().toCharacterArray())
     || Collections.contains(list2.get(i).toLowerCase().toCharacterArray(),list1.get(i).toLowerCase().toCharacterArray())){
        //then they are similar

    }
}

這將檢查其中一個字符串的字符數組是否包含在另一個字符串中。

嘗試這樣的事情:

List<String> l1 = Arrays.asList("iscat", "ishooman", "isdoge");
List<String> l2 = Arrays.asList("is_Cat", "is_Hooman", "is_doge");

System.out.println(l2.stream().map(s->s.toLowerCase().replace("_", "")).collect(Collectors.toList()).containsAll(l1));

上面的代碼使用流使用以下邏輯將字符串映射到所需格式: s->s.toLowerCase().replace("_", "") 如果有更多更改,則可以添加更多邏輯。


希望這可以幫助!

假設您將list2的一個元素轉換為小寫並刪除_,然后檢查list1中是否存在該元素,現在如果您對list2中的所有元素重復該操作並過濾該列表,那么可能會發生兩件事:

  1. 結果列表的大小與list1相同:這意味着所有List2元素都在list1中
  2. 結果列表的大小與list1的大小不同:意味着list1中至少不存在List2中的一個元素

List<String> myList = Arrays.asList("iscat", "ishooman", "isdoge");
List<String> myList2 = Arrays.asList("is_Cat", "is_Hooman", "is_Doge");
List<String> myListResult = new ArrayList<>(myList);
myListResult = myList2.stream().filter(x -> myList.contains(x.toLowerCase().replace("_", "")))
        .collect(Collectors.toList());

System.out.println(myListResult.size() == myList.size());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM