How to compare two Lists in Java?

To compare two Lists in Java, use the equals method.

Here's how you do it:

var firstList = java.util.List.of(
  "Hello",
  "World",
  "!"
);
var secondList = java.util.List.of(
  "Hello",
  "World"
);
var thirdList = java.util.List.of(
  "Hello",
  "World",
  "!"
);

boolean firstEqualsSecond = firstList.equals(secondList);
boolean firstEqualsThird = firstList.equals(thirdList);

// false
System.out.println(firstEqualsSecond);
// true
System.out.println(firstEqualsThird);