두 개의 집합, 중복 대상을 제거하고 새 대상을 남기다

4404 단어

방식 1
새 객체를 정의하고, 순환 컬렉션에서 데이터를 필터링하지 않고 새 객체에 추가하는 방법:
package test;

import com.dimensoft.server.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 *     ,      ,     
 */
public class Test2 {
    public static void main(String[] args) {
        List ulist = new ArrayList();
        User u1 = new User();
        u1.setId(null);
        u1.setIdCard("111");
        ulist.add(u1);

        User u2 = new User();
        u2.setId(null);
        u2.setIdCard("222");
        ulist.add(u2);

        User u5 = new User();
        u5.setId(null);
        u5.setIdCard("2");
        ulist.add(u5);

        User u6 = new User();
        u6.setId(null);
        u6.setIdCard("3");
        ulist.add(u6);


        List userList = new ArrayList();
        User u3 = new User();
        u3.setId(1);
        u3.setIdCard("111");
        userList.add(u3);

        User u4 = new User();
        u4.setId(2);
        u4.setIdCard("222");
        userList.add(u4);

        /*User u5 = new User();
        u5.setId(3);
        u5.setIdCard("2");
        userList.add(u5);

        User u6 = new User();
        u6.setId(4);
        u6.setIdCard("3");
        userList.add(u6);*/


        List newUserList = new ArrayList();
        for(User u: ulist){
            newUserList.add(u);
        }

        List newList = new ArrayList();


        for(User u: newUserList){
            boolean flag = true;
            for(User user: userList){   
                //       
                if(u.getIdCard().equals(user.getIdCard())){
                    flag = false;
                    break;
                }
            }
            if(flag){
                newList.add(u);
            }
        }

        for(User ue: newList){
            System.out.println(ue.getIdCard());
        }

    }
}

 
 
방식 2
list 집합에서remove 대상 방식
package test;

import com.dimensoft.server.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 *     ,      ,     
 */
public class Test {
    public static void main(String[] args) {
        List ulist = new ArrayList();
        User u1 = new User();
        u1.setId(null);
        u1.setIdCard("111");
        ulist.add(u1);

        User u2 = new User();
        u2.setId(null);
        u2.setIdCard("222");
        ulist.add(u2);

        User u5 = new User();
        u5.setId(null);
        u5.setIdCard("2");
        ulist.add(u5);

        User u6 = new User();
        u6.setId(null);
        u6.setIdCard("3");
        ulist.add(u6);


        List userList = new ArrayList();
        User u3 = new User();
        u3.setId(1);
        u3.setIdCard("111");
        userList.add(u3);

        User u4 = new User();
        u4.setId(2);
        u4.setIdCard("222");
        userList.add(u4);

        /*User u5 = new User();
        u5.setId(3);
        u5.setIdCard("2");
        userList.add(u5);

        User u6 = new User();
        u6.setId(4);
        u6.setIdCard("3");
        userList.add(u6);*/


        List newUserList = new ArrayList();
        for(User u: ulist){
            newUserList.add(u);
        }

        int i=0,j=0;
        for(User u: newUserList){
            System.out.println("-----------i="+i);
            for(User user: userList){
                System.out.println("-----------j="+j);
                //       
                if(u.getIdCard().equals(user.getIdCard()){
                    ulist.remove(u);
                }
                j++;
            }
            i++;
        }

        for(User ue: ulist){
            System.out.println(ue.getIdCard());
        }

    }
}

 
실체류
package test.entity;

import java.io.Serializable;

public class User implements Serializable {
	
	private Integer id;
	
	private String idCard;//     

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
}

좋은 웹페이지 즐겨찾기