How to compare equality of lists of arrays with modern Java?

后端 未结 5 1635
清酒与你
清酒与你 2021-01-31 07:18

I have two lists of arrays.

How do I easily compare equality of these with Java 8 and its features, without using external libraries? I am looking for a \"bett

5条回答
  •  甜味超标
    2021-01-31 07:46

    using zip (which originates from lambda b93) function from https://stackoverflow.com/a/23529010/755183, code could look like:

    boolean match = a.size() == b.size() && 
                    zip(a.stream(), b.stream(), Arrays::deepEquals).
                    allMatch(equal -> equal)
    

    update

    in order to check size of arrays first and then content this could be a solution to consider

    final boolean match = a.size() == b.size() 
                       && zip(a.stream(), b.stream(), (as, bs) -> as.length == bs.length).
                          allMatch(equal -> equal)
                       && zip(a.stream(), b.stream(), Arrays::deepEquals).
                          allMatch(equal -> equal);
    

提交回复
热议问题