Java List compare for equality

Introduction

The ArrayList class inherits its equals() method from the AbstractList class:

An ArrayList is equal to another object if

  • the other object implements the List interface
  • Two List objects haves the same size()
  • Two List objects contains the same elements in the same positions.

import java.util.Arrays;
import java.util.List;
public class Main {

  public static void main(String[] a) {

    List<String> list = Arrays.asList(new String[] { "CSS", "Binary", "C++", "Dart" });
    List<String> list2 = Arrays.asList(new String[] { "CSS", "Binary", "C++" });

    System.out.println(list.equals(list2));
  }/*from w  ww .ja v  a2s.com*/
}



PreviousNext

Related