如何比较两个不同的列表列表?

时间:2018-12-06 09:51:30

标签: java

列表示例:

    List1 =[[1,2,3],[4,5],[6,7],[8,9,10]]
    List2 =[[11,12],[13,14,15,16],[17,18,19],[20]]

我有两个列表列表,并且想要以以下方式比较列表元素

    1=>11, 2=>12,3=>No element present in List2
    4=>13, 5->14,15->No element present in List1,16->No element present in List1

,但条件是List 2可以包含比list1更多的元素。我如何才能相互比较两个列表?

我检查了在线解决方案,并尝试遍历for循环,但没有一个满足我的要求。

  1. list1和list2的大小相等,并且我已经添加了一个检查,仅在大小相等时才开始比较。

我可以在for循环的帮助下遍历两个列表,但是在比较中却很费力。

for (int m = 0; m < List1 .size(); m++) {

            for (int n = 0; n < List1 .get(m).size(); n++) {


                System.out.println("List1" + m + "==>" + List1 .get(m).get(n));

            }

            System.out.println("========================================================");
        }



        for (int i = 0; i < List2.size(); i++) {

            for (int k = 0; k < List2.get(i).size(); k++) {
                System.out.println("========================================================");
                System.out.println("List2" + i + "==>" + List2.get(i).get(k));
            }
        }

3 个答案:

答案 0 :(得分:1)

public static boolean isEqualsDeeply(List<List<Integer>> one, List<List<Integer>> two) {
    one = one != null ? one : Collections.emptyList();
    two = two != null ? two : Collections.emptyList();

    if (one.size() != two.size())
        return false;

    Iterator<List<Integer>> it1 = one.iterator();
    Iterator<List<Integer>> it2 = two.iterator();

    while (it1.hasNext() && it2.hasNext()) {
        List<Integer> subOne = it1.next();
        List<Integer> subTwo = it2.next();

        subOne = subOne != null ? subOne : Collections.emptyList();
        subTwo = subTwo != null ? subTwo : Collections.emptyList();

        if (subOne.size() != subTwo.size() || !subOne.equals(subTwo))
            return false;
    }

    return true;
}

PS

似乎对于Integer标准的equals也可以正常工作:

public static boolean isEqualsDeeply(List<List<Integer>> one, List<List<Integer>> two) {
    one = one != null ? one : Collections.emptyList();
    two = two != null ? two : Collections.emptyList();
    return one.equals(two);
}

答案 1 :(得分:0)

您可以使用<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script> <div class='col-8'> <h1 class="mt-2">Date Form</h1> <hr class="mt-0 mb-4"> <div class="form-group"> <div class='input-group date' id='datetimepicker1'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> <div class="form-group"> <div class='input-group date' id='datetimepicker2'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> </div>比较列表大小,并为嵌套的for循环获取最长的列表大小。示例:

Math.max(int n, int m)

答案 2 :(得分:-1)

不确定是否在检查列表是否还有另一个元素的java方面是否卡住,或者是否在逻辑上卡住。如果符合逻辑,这是完成工作的一些javascript代码:

var list1 = [[1,2,3],[4,5],[6,7],[8,9,10]];
var list2 = [[11,12],[13,14,15,16],[17,18,19],[20],[21]];

function compareLists(l1,l2){
    var i = 0;
    var maxLength = Math.max(list1.length,list2.length);
    for(i = 0; i< maxLength; i++){
        var sl1 = list1[i], sl2 = list2[i];
        compareSublists(sl1,sl2);
    }
}

function compareSublists(sl1,sl2){
    if(sl1 === undefined){
        console.log(sl2 + " has no corresponding sublist in list 1");
    }else if(sl2 === undefined){
        console.log(sl1 + " has no corresponding sublist in lis 2");
    }else{
        var i = 0;
        var maxLength = Math.max(sl1.length,sl2.length);
        for(i=0; i<maxLength;i++){
            var sl1e = sl1[i], sl2e = sl2[i];
            if(sl1e === undefined){
                console.log(sl2e+" has no element in list 1");
            }else if(sl2e === undefined){
                console.log(sl1e+" has no element in list 2");
            }else{
                console.log(sl1e + " => " + sl2e);
            }
        }
    }
}
compareLists(list1,list2);

转换为java应该不是太大的问题;但是多年以来我都没有接触过Java,所以抱歉,我不能帮您。