如何根据它包含的元素比较两个列表? [英] How to compare two list based on elements it contains?

查看:117
本文介绍了如何根据它包含的元素比较两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试类中,我有两个列表。一个是 expectedValue ,其他是 returnedType



我正在做

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

但是如何根据其元素的值类型比较两个列表?例如我在列表中有元素 sortOrder ,它有来自 1,2或3 的值,因此如何比较或说明对列表执行类似 assertEqual(expected,returned)的操作,并确保这两个列表具有相同的元素并具有相同的sortOrder含义元素也具有适当的排序格式?



注意:我不应该使用任何外部库。



更新



我的回报和预期列表示例:



excepted List = [代码:ANALYST,SortOrder:2,代码:STREET,SortOrder:1]
返回List = [代码:STREET, SortOrder:1,Code:ANALYST,SortOrder:2]



所以在这里非常基本的问题是如何基于一个列表的元素值,在我们的示例中,根据 sortOrder 值,所以我们的预期应为



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

解决方案

编辑:



最好的方法是做

  assertEquals (预期,返回); 

因为AbstractList使用成对比较实现了equals。
如果你不确定,你接受的List从AbstractList继承,你总是可以做

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

我留下的答案的其余部分为纪录目的(不要隐藏我可耻的无知) p>

您要声明列表的顺序也正确吗?

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

for(int i = 0; i {
assertEquals(expected.get .get(i));
}
}


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]

解决方案

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));
    }
}

这篇关于如何根据它包含的元素比较两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆