-1

Hello I am using selenium with java. I have to compare lists and return the difference.These values i am fetching from the database

List A has IDs and Income Tax, List B has Ids and Income tax.

I want to compare List A Ids with List B Ids and List A Income tax with List B Income tax. After compare I want to return the difference.i,e

Example :If IDS in List A [1,2,3] and Income Tax in List A are [10.25,1.58,30.28]

Ids in List B[1,2,3] and Income Tax in List B are [10.25,1.57,28.28]

I want to display which values are different after comparison and also for which IDs its different

I want output as Income tax is different for Ids 2 and 3.

For comparing the lists I tried the below code

try {
    IDList.equals(AfterChangeIDList);
        log.info("ID List are equal");
    }catch (Exception e) {
        log.warn("IDList are not equal");
        throw e;
    }

//Compare Income tax lists

try {
        IncomeTaxList.equals(AfterChangeIncomeTaxList);
        log.info("Income Tax list are equal");
    }catch (Exception e) {
        log.warn("Income Tax list are not equal");
        throw e;        
    }

May I know how to return the difference?

Sers
  • 12,047
  • 2
  • 12
  • 31
Suma
  • 103
  • 2
  • 14
  • Have you considered consulting the Javadoc? – user207421 Feb 04 '20 at 10:54
  • Why are you keeping these in separate lists instead of a map? A map would make it much easier to compare. – RealSkeptic Feb 04 '20 at 10:57
  • 1
    Add source code for `IDList` and `AfterChangeIDList`, because can't see what your `equals` method really does. Please rephrase/format your question - imagine someone sends you question in such format, would you even read it? – Piotr Niewinski Feb 04 '20 at 10:59
  • @RealSkeptic Thank you I will explore but may I get more information on this? – Suma Feb 04 '20 at 10:59
  • @user207421 No.Though I googled for solution I didn't get for this specific scenario. – Suma Feb 04 '20 at 10:59
  • @PiotrNiewinski: there is no source code for this as I have already mentioned that I am fetching these details from data base.My main question is not compare.I want to return the difference .If you can read my question carefully i have mentioned how I want my output to be – Suma Feb 04 '20 at 11:12
  • Yes it was similar.Thank you – Suma Feb 04 '20 at 12:22

2 Answers2

2

Please try the following:

public void test() {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        List<Double> taxes = new ArrayList<>();
        taxes.add(10.25d);
        taxes.add(1.58d);
        taxes.add(30.28d);
        List<Double> taxes2 = new ArrayList<>();
        taxes2.add(10.25d);
        taxes2.add(1.57d);
        taxes2.add(28.28d);

        List<Integer> differentIds = new LinkedList<>();
        for (int i = 0; i < ids.size(); i++) {
            if (!taxes.get(i).equals(taxes2.get(i))) {
                differentIds.add(ids.get(i));
            }
        }
        System.out.println("Different values for ids: " + differentIds);
    }
Piotr Niewinski
  • 1,298
  • 2
  • 15
  • 27
1
public static void main(String[] args) {

    List<Integer> ids = List.of(1, 2, 3);
    List<Double> taxes = List.of(10.25, 1.58, 30.28);
    List<Double> taxes2 = List.of(10.25, 1.57, 28.28);

    for (int i = 0; i < ids.size(); i++) {
        int id = i + 1;
        int result = taxes.get(i).compareTo(taxes2.get(i));

        if (result != 0) {
            System.out.println(
                    "value for id:" + id
                            + " is changed  old value: " + taxes.get(i)
                            + ",new value is: " + taxes2.get(i));
        }
    }
}
  • It worked for this particular data set, but it always compares with `taxes2.get(0)`, while it should have been `taxes2.get(i)` instead. For different data sets it will not work correctly. – Piotr Niewinski Feb 04 '20 at 13:25