Java JUnit5测试扁平ArrayList结果为false
为什么Junit测试向我显示AssertEquals对于我的测试是错误的 我正在扁平化这个结构,并针对它运行Junit5测试Java JUnit5测试扁平ArrayList结果为false,java,list,junit5,Java,List,Junit5,为什么Junit测试向我显示AssertEquals对于我的测试是错误的 我正在扁平化这个结构,并针对它运行Junit5测试 Arrays.asList("a", Arrays.asList("b", Arrays.asList("c", "d")), "e") Junit测试: @Test public void shouldFlattenAListOfList() throws Exception { List<String> flatten
Arrays.asList("a",
Arrays.asList("b",
Arrays.asList("c", "d")), "e")
Junit测试:
@Test
public void shouldFlattenAListOfList() throws Exception {
List<String> flatten = Problem07.flatten(Arrays.asList("a", Arrays.asList("b",
Arrays.asList("c", "d")), "e"), String.class);
assertEquals(flatten.size(), 5);
System.out.println(flatten == Arrays.asList("a", "b", "c", "d", "e")); // prints: false
assertEquals(flatten, Arrays.asList("a", "b", "c", "d", "e"));
}
带静态方法的普通类:
public class Problem07 {
static List<String> flatten(Collection<?> objects, Object aClass) {
if (objects == null) {
throw new NoSuchElementException();
}
if (objects.isEmpty()) {
return Collections.emptyList();
}
List<String> strings = new ArrayList<>();
/*TODO generify for other classes, not only hardcoded String*/
objects.forEach(o -> {
if (o instanceof ArrayList) {
ArrayList<String> o1 = (ArrayList<String>) o;
strings.addAll(o1);
} else {
strings.add(o.toString());
}
});
String formattedString = strings.toString()
.replace("[", "") //remove the right bracket
.replace("]", ""); //remove the left bracket
List<String> list = new ArrayList<>(Arrays.asList(formattedString.split(",")));
System.out.println(list);//prints: [a, b, c, d, e]
return list;
}
}
公共类问题07{
静态列表展平(集合对象、对象aClass){
if(objects==null){
抛出新的NoTouchElementException();
}
if(objects.isEmpty()){
返回集合。emptyList();
}
列表字符串=新的ArrayList();
/*TODO泛化其他类,而不仅仅是硬编码字符串*/
对象。forEach(o->{
if(o ArrayList的实例){
ArrayList o1=(ArrayList)o;
strings.addAll(o1);
}否则{
add(o.toString());
}
});
String formattedString=strings.toString()
.replace(“[”,”“)//拆下右支架
.replace(“]”,“”);//卸下左支架
列表列表=新的ArrayList(Arrays.asList(formattedString.split(“,”));
System.out.println(list);//prints:[a,b,c,d,e]
退货清单;
}
}
通过调用List.toString()
获得的formattedString
由于默认的toString()
格式设置,将在元素之间引入额外的空间。这意味着代替“a”、“b”、“c”。。。您的扁平列表将包含“a”、“b”、“c”。。。很明显,字符串“b”并不等于字符串“b”
您不应该依靠
toString()
和split()
来获得展开列表。您可以对其进行黑客攻击以删除表面空间,但最好使用递归来迭代对象集合中的每一层嵌套。我相信您扁平化数组的方式不建议您这样做。您正在使用strings.toString()
获取字符串,然后从中删除括号。我建议使用递归
对列表进行展平
。在这里,我使用递归修改了您的代码
static List<String> flatten(Collection<?> objects, Object aClass) {
if (objects == null) {
throw new NoSuchElementException();
}
if (objects.isEmpty()) {
return Collections.emptyList();
}
List<String> strings = new ArrayList<>();
objects.forEach(o -> {
if (o instanceof List) {
strings.addAll(flatten((List)o,String.class));
} else {
strings.add(o.toString());
}
});
return strings;
}
静态列表展平(集合对象、对象aClass){
if(objects==null){
抛出新的NoTouchElementException();
}
if(objects.isEmpty()){
返回集合。emptyList();
}
列表字符串=新的ArrayList();
对象。forEach(o->{
if(o instanceof List){
addAll(展平((List)o,String.class));
}否则{
add(o.toString());
}
});
返回字符串;
}
还有一个建议,请不要使用==
检查逻辑相等,而是使用equals
static List<String> flatten(Collection<?> objects, Object aClass) {
if (objects == null) {
throw new NoSuchElementException();
}
if (objects.isEmpty()) {
return Collections.emptyList();
}
List<String> strings = new ArrayList<>();
objects.forEach(o -> {
if (o instanceof List) {
strings.addAll(flatten((List)o,String.class));
} else {
strings.add(o.toString());
}
});
return strings;
}