web123456

Java method to determine whether two lists are equal

The following example can be regarded as a negative textbook, but it is actually wrong.

  public static boolean isEquals(List<String> list1,List<String> list2){
        if(null != list1 && null != list2){
            if((list2) && (list1)){
                return true;
            }
            return false;
        }
        return true;
    }

Gu thanks to the blogger: @A small tree that keeps the wind/zudama3458】Authorized solution to avoid misleading more friends by the typical error code above.

1 Join maven dependencies

   <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
    </dependency>

2. Sample code

public static void main(String[] args) {
         List<String> list1 = new ArrayList<>();
         List<String> list2 = new ArrayList<>();
         ("1");
         ("1");
         ("1");
         boolean isequal = (list1,list2); //Return true if it is equal
         (isequal);
 }