How to compare two list based on elements it contains?

夙愿已清 提交于 2019-12-25 03:55:29

问题


In my unit test class, I am having two list. One is the expectedValue and other is returnedType.

I am doing

Collections.sort(expected);
Collections.sort(returned);

but how do I compare two list based on the type of value of one of its element? For example I have element sortOrder in both the list and it has values from 1,2 or 3 so how do i compare or say doing something like assertEqual(expected, returned) for both the list and make sure that both list has same elements and has same sortOrder meanings elements are also in proper sort format?

Note: I should not be using any external libraries for doing it.

Update

Example of my return and expected list:

excepted List = [Code: ANALYST, SortOrder: 2, Code: STREET, SortOrder: 1] and returned List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]

So at very basic question here is how can I sort a list based on one of its element value, in our example according to sortOrder value so our expected should be

excepted List = [Code: STREET, SortOrder: 1, Code: ANALYST, SortOrder: 2]


回答1:


Edit:

Best way is to do

assertEquals(expected, returned);

because AbstractList implements equals using pairwise comparison. If you are not sure, the Lists you are receiving inherit from AbstractList, you can always do

assertEquals(new ArrayList(expected), new ArrayList(returned));

I leave the rest of the answer for documentary purposes (and to not hide my shameful ignorance)

Do you want to assert that the lists are in the correct order also?

public static void assertEquals(List expected, List returned)
{
    assertEquals(expected.size(), returned.size());
    if(!expected.containsAll(returned))
    {
       fail();
    }

    for(int i = 0; i < expected.size(); i++)
    {
        assertEquals(expected.get(i),returned.get(i));
    }
}



回答2:


Have a look at Hamcrest - library of matchers for building test expressions.

It offers lots of methods to compare Collections (and other hard-to-compare classes) and special matchers that tell you why the collections didn't match while throwing AssertionErrors




回答3:


Use equals().




回答4:


To expand on Bohemian's answer (+1), Hamcrest is a very good way to do this. Here's an example:

assertThat(myListOfIntegers, contains(1, 2, 3));

Matchers.contains asserts that the list elements are the right type, the right quantity, and in the right order. Therefore, in the above assertion i'm expecting myListOfIntegers to be the exact list: [1, 2, 3].




回答5:


Make sure the objects in the list override the equals and hashcode methods, but otherwise you can just compare the lists directly as per the contract of java.util.List which states that they are equal if the lists contain the same objects in the same order.

http://download.oracle.com/javase/6/docs/api/java/util/List.html#equals(java.lang.Object)




回答6:


Try to use this: CollectionUtils.isEqualCollection(expected, returned);



来源:https://stackoverflow.com/questions/6852117/how-to-compare-two-list-based-on-elements-it-contains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!