How to compare two lists equal in java
Table of Contents
How to compare two lists either equal or not by using list equal method in java. If two lists are equal then it will true else it will return false. List following the index order and comparing the data. String type List following Case Sensitive. So be care full while using String type List.
Here I am sharing examples for how to compare two Integer type lists with equal and without equal, and two String type Lists.
ListEqaulWithAnotherList.java
package com.narayanatutorial.list; import java.util.ArrayList; import java.util.List; public class ListEqaulWithAnotherList { public static void main(String[] args) { // Initializing list List<Integer> firstList = new ArrayList<>(); firstList.add(30); firstList.add(10); firstList.add(50); System.out.println("firstList:" + firstList); // Initializing another list List<Integer> secondList = new ArrayList<>(); secondList.add(30); secondList.add(10); secondList.add(50); secondList.add(60); System.out.println("secondList:" + secondList); if (firstList.equals(secondList)) { System.out.println("both list are Equal"); } else { System.out.println("both list are Not equal"); } } }
ListNotEqaulWithAnotherList.java
package com.narayanatutorial.list; import java.util.ArrayList; import java.util.List; public class ListNotEqaulWithAnotherList { public static void main(String[] args) { // Initializing list List<Integer> firstList = new ArrayList<>(); firstList.add(30); firstList.add(10); firstList.add(50); System.out.println("firstList:" + firstList); // Initializing another list List<Integer> secondList = new ArrayList<>(); secondList.add(10); secondList.add(30); secondList.add(50); System.out.println("secondList:" + secondList); if (firstList.equals(secondList)) { System.out.println("both list are Equal"); } else { System.out.println("both list are Not equal"); } } }
ListNotEqaulWithAnotherListStringCaseSense.java
package com.narayanatutorial.list; import java.util.ArrayList; import java.util.List; public class ListNotEqaulWithAnotherListStringCaseSense { public static void main(String[] args) { // Initializing list List<String> firstList = new ArrayList<>(); firstList.add("Narayana"); //Here Tutorial, T is in small firstList.add("tutorial"); System.out.println("firstList:" + firstList); // Initializing another list List<String> secondList = new ArrayList<>(); secondList.add("Narayana"); //Here Tutorial, T is in Caps secondList.add("Tutorial"); System.out.println("secondList:" + secondList); if (firstList.equals(secondList)) { System.out.println("both list are Equal"); } else { System.out.println("both list are Not equal"); } } }
Get complete source code from GitHub
Please give comments and subscribe to get the latest updates.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.