public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[")
.append("id=").append(this.id).append(",")
.append("username=").append(this.username).append(",")
.append("password=").append(this.password).append("]");
return sb.toString();
}
}
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername("杨过");
user1.setPassword("yangguo");
user1.setId(100);//模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername("小龙女");
user2.setPassword("longnv");
user2.setId(120);//模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername("李莫愁");
user3.setPassword("mochou");
user3.setId(110);//模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername("張無忌");
user4.setPassword("wuji");
user4.setId(129);//模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername("黄蓉");
user1.setPassword("huangrong");
user1.setId(0);//
volist.add(user1);
// update
User user2 = new User();
user2.setUsername("龙女");
user2.setPassword("longnv");
user2.setId(120);//更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername("李莫愁");
user3.setPassword("mochou");
user3.setId(110);//模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist);// update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println("insert"+out);
}else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println("update"+out);
}else{
System.out.println("delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
import java.util.ArrayList;
import java.util.List;
public class CompareList {
public static List<User> initlistPO(){
List<User> polist = new ArrayList<User>();
User user1 = new User();
user1.setUsername("杨过");
user1.setPassword("yangguo");
user1.setId(100);//模拟从持久层存在的数据,所以存在ID
polist.add(user1);
User user2 = new User();
user2.setUsername("小龙女");
user2.setPassword("longnv");
user2.setId(120);//模拟从持久层存在的数据,所以存在ID
polist.add(user2);
User user3 = new User();
user3.setUsername("李莫愁");
user3.setPassword("mochou");
user3.setId(110);//模拟从持久层存在的数据,所以存在ID
polist.add(user3);
User user4 = new User();
user4.setUsername("張無忌");
user4.setPassword("wuji");
user4.setId(129);//模拟从持久层存在的数据,所以存在ID
polist.add(user4);
return polist;
}
public static List<User> initlistVO(){
// add
List<User> volist = new ArrayList<User>();
User user1 = new User();
user1.setUsername("黄蓉");
user1.setPassword("huangrong");
user1.setId(0);//
volist.add(user1);
// update
User user2 = new User();
user2.setUsername("龙女");
user2.setPassword("longnv");
user2.setId(120);//更新字段
volist.add(user2);
//no operation
User user3 = new User();
user3.setUsername("李莫愁");
user3.setPassword("mochou");
user3.setId(110);//模拟从持久层存在的数据,所以存在ID
volist.add(user3);
/* [至若]这样的对象应该不会存在的
User user4 = new User();
user4.setUsername("周芷若");
user4.setPassword("zhiruo");
user4.setId(112);//
volist.add(user4);*/
// delete yangguo
return volist;
}
public static void updateOrsave(){
List<User> polist = initlistPO();
List<User> volist = initlistVO();
List<User> pobacklist = new ArrayList<User>();
pobacklist.addAll(polist);
polist.removeAll(volist);// update and delete
volist.removeAll(pobacklist);
for(User out :volist){
if(out.getId()==0){
System.out.println("insert"+out);
}else{
for(User inner:polist){
if(out.getId() == inner.getId()){
System.out.println("update"+out);
}else{
System.out.println("delete"+inner);
}
}
}
}
}
public static void printList(List<User> list){
for(User user:list){
System.out.println(user);
}
}
public static void main(String args[]){
updateOrsave();
}
}
delete[id=100,username=杨过,password=yangguo]
update[id=120,username=龙女,password=longnv]
delete[id=129,username=張無忌,password=wuji]