需要比较两个列表。 列表可以包含null和重复的(Object :: equals)项目。
{4,100,100}等于{100,4,100}
{4,100,100}不等于{100,4,4}
{“ W”,“ s”,“ s”,null,null}等于{null,“ s”,null,“ s”,“ W”}
方法:
BiPredicate<List<?>, List<?>> compareList = (a, b) -> ???;
测试:
List<String> listS = Arrays.asList("W", "s", "s", null, null);
List<String> listT = Arrays.asList(null, "s", null, "s", "W");
assertTrue(compareList.test(listS, listT));
List<Integer> listA = Arrays.asList(4, 100, 100);
List<Integer> listB = Arrays.asList(100, 4, 100);
List<Integer> listC = Arrays.asList(100, 4, 4);
assertTrue(compareList.test(listA, listB));
assertFalse(compareList.test(listA, listC));
答案 0 :(得分:1)
您可以使用org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder()
检查包含以下内容的列表
assertThat("List equality without order",
listA, containsInAnyOrder(listB.toArray()));
答案 1 :(得分:0)
使用AssertJ和containsExactlyInAnyOrder()
List<String> list = Arrays.asList(null, "s", null, "s", "W");
assertThat(list).containsExactlyInAnyOrder("W", "s", "s", null, null);
不需要为基本的Java类型编写自己的断言库。已经有一些:AssertJ,Hamcrest等。