0

I have a class B which extends class A

abstract class A{
 Integer val;
}
class B extends A {...}

Now there is a list1 of class A objects, which I need to compare and check if they are equal in values and order as well, with another list2 I get after I apply a certain sort on the list1

B var1 = new B();
B.setVal(1);
B var2 = new B();
B.setVal(null);
B var3 = new B();
B.setVal(10);
B var4 = new B();
B.setVal(15);
List<B> list1 = Lists.newArrayList();

Now I have a sort method which gives list2

list2 = [null,1,5,10]

What method can I use to verify list1<>list2 ?

When I tried equals(), it is giving me list1 == list2

Nija Saju
  • 1
  • 2
  • 1
    is `setVal` a `static` method or why are you calling it on the class and not the objects you create? – OH GOD SPIDERS Jan 20 '21 at 14:03
  • `List.equals` has the behaviour you're asking for. Please share a minimal reproducible example so we can understand exactly why you're not getting the expected behaviour. – MikeFHay Jan 20 '21 at 14:36
  • Which `sort` method did you use and how you get `list2`? For example, `Collections.sort` modifies the input list. – Nowhere Man Jan 20 '21 at 15:48

2 Answers2

0

This might work for you:

import java.util.*;
import java.util.stream.Collectors;


abstract class A{
    Integer val;

    public void setVal(Integer val) {
        this.val = val;
    }

    public Integer getVal() {
        return val;
    }
}
class B extends A {

}
public class Test {

    public static void main(String[] args){
        List<B> list1 = new ArrayList<>();
        B var1 = new B();
        var1.setVal(1);
        B var2 = new B();
        var2.setVal(null);
        B var3 = new B();
        var3.setVal(10);
        B var4 = new B();
        var4.setVal(5);
        list1.add(var1);
        list1.add(var2);
        list1.add(var3);
        list1.add(var4);
        List<Integer> list2 = Arrays.asList(new Integer[]{null,1,5,10});
        List<Integer> list3 = list1.stream().map(B::getVal).collect(Collectors.toList());


        System.out.println("Both list are equal "+list3.containsAll(list2));
    }

}

Here I have converted bist of B's object to list of B.val and then checked whether all the value in list1 and list2 are equal.

Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53
0

If you need to compare instance of List<B> to List<Integer> taking the order of the elements into account, first you need to check if both lists have the same size and then compare each element in both lists at appropriate position using Objects.equals which may return true if both arguments are null:

static boolean listsAreEqual(List<B> listB, List<Integer> listInts) {
    return listB.size() != listInts.size() ? false : 
        IntStream.range(0, listB.size())
                 .allMatch(i -> Objects.equals(listB.get(i).getVal(), listInts.get(i)));
}

Additionally, you may need to assume the lists equal when some instance of B is null (not just its val field) and appropriate Integer is null:

static boolean listsAreEqualWithNulls(List<B> listB, List<Integer> listInts) {
    return listB.size() != listInts.size() ? false : 
        IntStream.range(0, listB.size())
                 .allMatch(i -> 
                     Objects.equals(
                         Optional.ofNullable(listB.get(i)).map(B::getVal).orElse(null),
                         listInts.get(i)
    ));
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42