如何测试字符串数组列表的相等性 [英] How to test for equality of Lists of String arrays

查看:52
本文介绍了如何测试字符串数组列表的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我看不到树木茂密的森林...如何在字符串数组列表中递归测试所有元素的相等性(而非同一性)?这是一个最小的示例:

I think I can't see the forest for the trees... How do I test for equality (not identity) of all elements recursively in a List of String arrays? Here's a minimal example:

List<String[]> left = new ArrayList<>();
left.add(new String[]{"one", "two"});

List<String[]> right = new ArrayList<>();
right.add(new String[]{"one", "two"});

System.out.println(left.equals(right) ? "Yeah!" : "This is not what I want.");

我想在单元测试中使用它.

I want to use this in unit tests.

更多上下文:我有一个类,其中包含几个字段,其中一些是 String 数组的 List ,而有些是 Set String 数组.该类是解析器的结果(实际上是解析器级联中的中间步骤).为了测试解析器,我想检查所讨论类的实例是否相等.IDE自动生成的 equals 实现对多个 List< String []> Set<; String []> 字段,所以我想到了-等效于上面提供的最小示例.

A bit more context: I have a class that holds several fields, some of which are List of String arrays and some are Sets of String arrays. The class is a result of a parser (actually an intermediate step in a cascade of parsers). In order to test the parser I want to check for equality of instances of the class in question. The IDE-autogenerated equals implementation uses a cascade of Objects.equals invocations on the several List<String[]> and Set<String[]> fields, which is---so I figured---equivalent to the minimal example that I've provided above.

推荐答案

Java文档要求列表 equals()能够在您使用时起作用,因此,如果这些代码< Lists 的code> List :

The Java documentation requires list equals() to behave as you're using it, so your code would work if these were Lists of Lists:

布尔值等于(对象o)

将指定的对象与此列表进行比较以确保相等.当且仅当指定对象也是一个列表,并且两个列表具有相同的大小,并且两个列表中所有对应的元素对相等时,才返回true.(如果(e1 == null?e2 == null:e1.equals(e2)),则两个元素e1和e2相等.)换句话说,如果两个列表包含相同顺序的相同元素,则两个列表定义为相等..此定义可确保equals方法可在List接口的不同实现中正常工作.

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

但是,在叶子"处有简单的Java数组.这些将使用 equals Object 定义,该定义将测试引用身份,而不是值相等.

However at the "leaves" you have plain Java arrays. These will use the Object definition of equals, which tests for reference identity, not value equality.

在测试中解决此问题的一种方法是编写一个小的帮助程序工厂例程,该例程接受 String s的可变参数列表并产生 List 返回以替换Java数组您当前正在使用.然后所有人都应该工作正常.您的示例如下所示:

One way around this in your tests is to write a little helper factory routine that accepts a variable argument list of Strings and produces List returns to replace the Java arrays you're currently using. Then all ought to work fine. Your example would look like this:

// A little helper to avoid the awkward syntax
// Arrays.asList(new String [] { "one", "two" })
public static List<String> listOf(String ... strings) {
    return Arrays.asList(strings);
}

public static void main(String[] args) {
    List<List<String>> left = new ArrayList<>();
    left.add(listOf("one", "two"));

    List<List<String>> right = new ArrayList<>();
    right.add(listOf("one", "two"));

    System.out.println(left.equals(right) ? "Yeah!" : "This is not what I want.");
}

这篇关于如何测试字符串数组列表的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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