簡體   English   中英

遍歷兩個列表時如何檢查字符串是否相等?

[英]How to check String equality while iterating over two lists?

我有兩個 java 類:

public class MyClass1 {
  private String userId;
  private String userName;
  private List<CustomList1> customList1;

  // getters and setters
  // inner CustomList1 class
}

public class MyClass2 {
  private String userId;
  private List<CustomList2> customList2;

  // getters and setters
  // inner CustomList2 class
}

現在,我有這些類的列表:

List<MyClass1> classOneList;
List<MyClass2> classTwoList;

classOneListclassTwoList列表中,object 應按userId升序排序。 兩個列表中的userId應該具有相同的值。 我要檢查的是:

  1. 兩個列表的大小相同嗎? 如果沒有,就拋出錯誤異常。
  2. 兩者中的每個下一個元素是否都列出了相同的userId 如果不是,則拋出另一個異常。

第 1 步。我已經完成了簡單的 if 語句。

根據原型,第 2 步應該如下所示:

for (el1, el2 : classOneList, classTwoList) {
  el1.getUserId().isEqualTo(el2.getUserId());
}

嘗試以下代碼解決您的問題。

public class Testing {

    public static void main(String[] args) {
        Map<String, List<String>> map1 = new LinkedHashMap<String, List<String>>();
        List<String> m1l1 = new LinkedList<String>();
        m1l1.add("One");
        m1l1.add("Two");
        m1l1.add("Three");
        m1l1.add("Four");

        map1.put("1", m1l1);

        List<String> m1l2 = new LinkedList<String>();
        m1l2.add("One");
        m1l2.add("Two");
        m1l2.add("Three");
        m1l2.add("Four");

        map1.put("2", m1l2);

        // Add more element into the map1 by creating more list.

        Map<String, List<String>> map2 = new LinkedHashMap<String, List<String>>();
        List<String> m2l1 = new LinkedList<String>();
        m2l1.add("One");
        m2l1.add("Two");
        m2l1.add("Three");
        m2l1.add("Four");

        map2.put("1", m2l1);

        // Add more element into the map2 by creating more list.

        for (Entry<String, List<String>> entry : map1.entrySet()) {
            if (map2.containsKey(entry.getKey())) {
                if (entry.getValue().size() == map2.get(entry.getKey()).size()) {

                } else {
                    System.out.println("UserId are same but list are different for userid: " + entry.getKey());
                }
            }
            else {
                System.out.println("Userid '"+entry.getKey()+"' exists in map1 but is not found in map2");
            }
        }

    }
}

希望這可以幫助你。

 if(classOneList.size() != classTwoList.size()){
        throw new ErrorException();
    }else{
        classOneList = classOneList.stream().sorted(Comparator.comparing(MyClass1::getUserId)).collect(Collectors.toList());
        classTwoList = classTwoList.stream().sorted(Comparator.comparing(MyClass2::getUserId)).collect(Collectors.toList());
        for (int i = 0; i < classOneList.size(); i++){
            if(!classOneList.get(i).getUserId().equals(classTwoList.get(i).getUserId())){
                throw new AnotherErrorException();
            }
        }
    }

暫無
暫無

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

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