将两个List合并为一个List(并集)+取交集

并集

List questionerIdList = new ArrayList();

List l1 = sysuserinfMapper.findSysuserIdsByrRealName(questionName);

List l2 = wechatinfMapper.selectWechatIdsByNickname(questionName);

questionerIdList.addAll(l1);
questionerIdList.addAll(l2);

                             //准备一个空的list,将两个list添加进去

StringBuffer ids = new StringBuffer();

if(questionerIdList.size()==0||questionerIdList==null){
return 0;
 }else{

 for (int i = 0; i < questionerIdList.size(); i++) {
if (i != 0) {
ids.append(",");
}
ids.append(questionerIdList.get(i));
}

 }

                            //将list转化为以,分隔开的形式的字符串(1,2,3,4,5)


********************************************************************************************

********************************************************************************************


Map params = new HashMap<>();

params.put("ids", ids.toString());

List cclist = consultCustomerMapper.selectConsultCustomersByCondition(params);

                           //serviceimpl中调用mapper中方法,并将参数params传进去


select * from 表 where 字段  in (${ids})  

                            // mapper.xml  中sql语句





交集

@org.junit.Test
	public void add() throws Exception {

		List l1 = new ArrayList();
		List l2 = new ArrayList();
		List l3 = new ArrayList();
		List l4 = new ArrayList();
		
		l1.add(1);
		l1.add(2);
		l1.add(3);
		l1.add(4);
		l1.add(5);
		
		l2.add(1);
		l2.add(2);
		l2.add(3);
		
		l3.add(1);
		l3.add(3);
		l3.add(4);
		
		l4.add(1);
		l4.add(2);
		l4.add(3);
		l4.add(4);
		l4.add(5);
		l4.add(6);
		
		l2.retainAll(l1);
		l2.retainAll(l3);
		l2.retainAll(l4);
		
		System.out.println("----");
		System.out.println(l2);
		
	}
        l2. retainAll(l1);
        l2. retainAll(l3);
        l2.retainAll(l4);  之后的l2即为交集

你可能感兴趣的:(SSM,Java,SSH)