Java 为什么要列出这两个列表<;E>;不平等吗?
我有一个简单的java程序,需要测试POJO元素的两个列表是否相等Java 为什么要列出这两个列表<;E>;不平等吗?,java,list,equals,contains,Java,List,Equals,Contains,我有一个简单的java程序,需要测试POJO元素的两个列表是否相等 public void i_have_following(List<Post> myPost) throws Throwable { //retrieve list of element from rest end point String url = "http://jsonplaceholder.typicode.com/posts/"; RestTemplate
public void i_have_following(List<Post> myPost) throws Throwable {
//retrieve list of element from rest end point
String url = "http://jsonplaceholder.typicode.com/posts/";
RestTemplate restTemplate = new RestTemplate();
ParameterizedTypeReference<List<Post>> responseType = new ParameterizedTypeReference<List<Post>>() {
};
ResponseEntity<List<Post>> responseEntity = restTemplate.exchange(url,
HttpMethod.GET, null, responseType);
List<Post> allPosts = responseEntity.getBody();
//get size=1 sublist of it and compare with expected local value
List<Post> firstPost = allPosts.subList(0, 1);
System.out.println("size of firstPost = " + firstPost.size());
System.out.println("size of myPost = " + myPost.size());
Assert.assertEquals(firstPost.size(), myPost.size());
Assert.assertEquals(firstPost.get(0).getUserId(), myPost.get(0)
.getUserId());
Assert.assertEquals(firstPost.get(0).getId(), myPost.get(0).getId());
Assert.assertEquals(firstPost.get(0).getTitle(), myPost.get(0)
.getTitle());
Assert.assertEquals(firstPost.get(0).getBody(), myPost.get(0).getBody());
Assert.assertTrue(firstPost.equals(myPost)); //FAIL!!
Assert.assertTrue(firstPost.containsAll(myPost)); //FAIL!!
}
元素“POST”只是一个简单的POJO元素:
public class Post {
private Integer userId;
private Integer id;
private String title;
private String body;
/**
*
* @return The userId
*/
public Integer getUserId() {
return userId;
}
/**
*
* @param userId
* The userId
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
*
* @return The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return The body
*/
public String getBody() {
return body;
}
/**
*
* @param body
* The body
*/
public void setBody(String body) {
this.body = body;
}
}
从打印输出和断言中可以明显看出,这两个列表具有相同的size=1,并且它们唯一的列表元素具有相同的值(最后两个之前的所有断言都为TRUE)
我真的很困惑,为什么最后两个断言会失败。我假设equals()和containsAll()是用于列表比较的常用api,并且我正确地使用了它们
谁能给我一个提示,这里缺少什么
非常感谢。如果比较两个相同的项目,则应重写方法equals或使用Java Compariable接口:
那么
Assert.assertEquals(firstPost.get(0),mytPost.get(0)
呢?我敢打赌它失败了……你的两个对象并不相等。这些评论应该出现在4chan的头版……谢谢。你能提供更多的信息吗?我想POJO有一个基于其成员变量的equal()的默认equal().参见此处:或此处:
public class Post {
private Integer userId;
private Integer id;
private String title;
private String body;
/**
*
* @return The userId
*/
public Integer getUserId() {
return userId;
}
/**
*
* @param userId
* The userId
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
*
* @return The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return The body
*/
public String getBody() {
return body;
}
/**
*
* @param body
* The body
*/
public void setBody(String body) {
this.body = body;
}
}