您的位置:首页 > 其它

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

2017-03-30 14:33 447 查看

并集

List<Integer> questionerIdList = new ArrayList<Integer>();

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<ConsultCustomer> cclist = consultCustomerMapper.selectConsultCustomersByCondition(params);

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

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

                            // mapper.xml  中sql语句

交集

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

List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
List<Integer> l3 = new ArrayList<Integer>();
List<Integer> l4 = new ArrayList<Integer>();

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即为交集
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: