-2

I have two arraylist's, both have 3 elements each. PL1 and INITIAL1 are to array list's. INITIAL1 will be initialized with values, PL1 will be edited dynamically. If both are equal, then an operation will be performed else nothing.

PL1 = [00, 01, 02]
INITIAL1 = [01, 02, 00]

Values are same but in different order. are these two equal?

will I be able to conclude that both are equivalent and perform some operation?

SDJ
  • 4,083
  • 1
  • 17
  • 35
Deep Chhowala
  • 99
  • 2
  • 11

4 Answers4

1

Two Lists are considered equal if elements in each List are exactly the same by equals method including their order in Lists. So to answer your questions,

Values are same but in different order. are these two equal?

Since they are in a different order, they are not equal.

will I be able to conclude that both are equivalent and perform some operation?

I don't know why you need to do this but it is possible. You can use HashMap to store INITIALS as key and check if size of PL1 is same as Map's size and Map contains all values in PL1 by equals check.

Or Simply use Apache Commons CollectionUtils.isEqualCollection(Collection a, Collection b)

0
if (INITIAL1.size() == PL1.size() && INITIAL1.containsAll(PL1)) {
  //do something
}
ivarkol
  • 39
  • 3
0

Two array list in java are said to be equal when the contain the same number of element, with same content and in same order. To check it you can use .equals() function

for example:

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
      ArrayList<String> list1=new ArrayList<String>();//Creating arraylist 
      list1.add("Ravi");//Adding object in arraylist    
      list1.add("Vijay");    
      list1.add("Ravi");    
      list1.add("Ajay");    
      //Invoking arraylist object   
      System.out.println(list1); 

      ArrayList<String> list2=new ArrayList<String>();//Creating arraylist 
      list2.add("Ravi");//Adding object in arraylist    
      list2.add("Vijay");    
      list2.add("Ravi");    
      list2.add("Ajay");    
      //Invoking arraylist object   
      System.out.println(list2); 

      if(list1.equals(list2)) {
          System.out.println("yes");
      }
     }
}

your Array list won't evaluate to equal since they have different element in different order.

and if you want that they evaluate to true without sorting then checkout this link

Java ArrayList - how can I tell if two lists are equal, order not mattering?

Ayush Mishra
  • 267
  • 3
  • 14
0
package com.practice;    
import java.util.ArrayList;
import java.util.List;
public class twoArray 
{
    public static void main(String[] args) 
    {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        list1.add(00);
        list1.add(01);
        list1.add(02);
        System.out.println(list1 +" Array list 1");   // [00,01,02] Array list 1

        list2.add(01);
        list2.add(02);
        list2.add(00);
        System.out.println(list2 +" Array list 2");   // [01,02,00] Array list 2

        System.out.println(list1.equals(list2));       // output: **false**         
    }
}

I hope after going through this example you got an idea. As the ArrayList internally uses the index (position) to store the values. As per your requirements [00,01,02] and [01,02,00] the values are same and the ordering is different and hence your using ArrayList it returns false! And it's not equal and and you won't be able to perform any operations.

The best solution for this is you just sort both PL1 and INITIAL1 and it returns true.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sunil
  • 71
  • 1
  • 11