0

I need to compare data retrieved from table and a list :

      // first click

      // get list of data
      originalList = getValue(columnX);


      // copy
      List<String> copy1 = new ArrayList<String>(originalList );

      //sort
      Collections.sort(copy1);
      System.out.println("copy" + copy1);

    // verify
     assertEquals(copy1, originalList);  //this is OK

    //second click is done here

    // copy
    List<String> copy2 = new ArrayList<String>(originalList ); 


  // sort
  Collections.sort(copy2 );
  System.out.println("copy2" + copy2 );=>copy2 give same values as copy1!!!it should  not

// verify assertEquals(copy2, originalList); =>wrong

lola
  • 5,649
  • 11
  • 49
  • 61
  • ? what does it mean? the copy2 i created it gives me same values as copy , and it should be other values as I have clicked a second time – lola Oct 03 '11 at 16:05
  • 2
    In between the construction of copy and copy2, originalList doesn't change... so why *shouldnt* sorting copy and copy2 yield the same result? – claymore1977 Oct 03 '11 at 16:05
  • the first assert is OK but if I try a second assert on copy2, this fails as copy2 gives same results as copy – lola Oct 03 '11 at 16:06
  • 1
    assertEquals is comparing *references*, not *contents* of the ArrayLists. See http://www.java2s.com/Code/Java/Development-Class/SimpleuseofJUnittotestArrayList.htm. And your question is still quite unclear. – mellamokb Oct 03 '11 at 16:10
  • Actually, when invoked with two objects, `assertEquals` does use the `equals` method, but ArrayList's `equals` will only return `true` if both lists have the same elements on _the same order_. – PaoloVictor Oct 03 '11 at 16:13
  • You need to copy each element of the list individually ... [duplicate of this][1] [1]: http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents – NimChimpsky Oct 03 '11 at 16:14
  • I have a list of data , I click to sort it , make a copy 1 and compare then click a second time to verify the sorting ,create a copy2 then use assertEquals, the second copy is wrong – lola Oct 03 '11 at 16:15
  • i would like that copy2 five me correct data as it is the case for copy. – lola Oct 03 '11 at 16:16
  • 1
    Show us your `assertEquals` code – Steve Kuo Oct 03 '11 at 16:17
  • clone on original list??? i don't thin this is what I 'm lookinng for as originalList gives me a correst data – lola Oct 03 '11 at 16:18
  • @NimChimpsky: No, because these are strings, which are immutable. There's no need to clone them. – Jon Skeet Oct 03 '11 at 16:18
  • @lola: Please show a short but complete program to demonstrate the problem. No data tables, no clicking - just taking the list, copying it, sorting etc. – Jon Skeet Oct 03 '11 at 16:19
  • @Steve, Ive updated the problem by adding second assert , in fact this is the result I have : after second click[2, 2, 1, 1, 1, 0, 0, 0, 0] copy2[0, 0, 0, 0, 1, 1, 1, 2, 2] doesn't match first list – lola Oct 03 '11 at 16:20

1 Answers1

0

I guess that the first assertEquals succeeded because the data in originalList was already sorted. Sorting copy1 again did not change the order, hence equals returns true.

The second click did change the order (I assume that the list is now in reverse order). Hence the second assertEquals fails - originalList and copy2 do contain the same elements but in different order.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • Depends on what you want. The original question (now edited away) was: "how could I make safely a second copy?" The answer is: Technically you did, but your definition of "safe" seems to be different than that of Java. What is your definition? – A.H. Oct 03 '11 at 16:33
  • first values:OK, copy1 is OK after sort, second values after click is OK, copy2after sort is equal to copy 1??? – lola Oct 03 '11 at 16:35
  • For example. That would test, that still the same elements are there. Additionally you could test, if the second click did revert the order: Compare also to `Collections.revert(copy1); assertEquals(originalList, copy1)` – A.H. Oct 03 '11 at 16:40