1简单元素的查找

一、找到2个集合中相同和不同的元素

本文分享的示例代码实现提取2个集合中相同和不同的元素

此处需要使用Collection集合所提供的一个方法:removeAll(Cellection list),removeAll方法用于从列表中移除指定collection中包含的所有元素。

语法 removeAll(Collection<?> c)

c:包含从列表中移除元素的collection对象。

该方法返回值为boolean对象,如果List集合对象由于调用removeAll方法而发生更改,则返回true,否则返回false

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Test {
    public static void main(String args[]){
        //集合一
        List _first=new ArrayList();
        _first.add("jim");
        _first.add("tom");
        _first.add("jack");
        //集合二
        List _second=new ArrayList();
        _second.add("jack");
        _second.add("happy");
        _second.add("sun");
        _second.add("good");
        Collection exists=new ArrayList(_second);
        Collection notexists=new ArrayList(_second);
        exists.removeAll(_first);
        System.out.println("_second中不存在于_set中的:"+exists);
        notexists.removeAll(exists);
        System.out.println("_second中存在于_set中的:"+notexists);
    }
}

结果:

_second中不存在于_set中的元素:[happy, sun, good]
    _second中存在于_set中的元素:[jack]

二、去除List中的重复元素(此处只举最简单、常用的方法)

利用HashSet元素不重复的特性(如果泛型是对象,那么需要实现equals和hashCode方法)



public void testOtherList(){
//新建List集合
List nowList=new ArrayList();
//加入元素
nowList.add(1);
nowList.add(2);
nowList.add(2);
nowList.add(55);
nowList.add(3);
nowList.add(1);
nowList.add(56);
nowList.add(56);
//利用HashSet元素不重复的特性
nowList=new ArrayList(new HashSet(nowList));
System.out.println("去除重复数据后的集合:"+nowList);

三、操作集合,求交集、并集和差集

package day0527;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 
 * @ClassName: FindNumber
 * @Description: A={6,3,9,3,2,4,5,7},B={5,8,6,2,1,9},则输出3,4,7,1,8 思路:全集除掉交集,就是结果
 * @author
 * @date 2016年5月27日 上午9:56:25
 *
 */
public class FindNumber {
public static void main(String[] args) {
// 注意:一定要使用创建对象的格式创建数组
Integer[] a = new Integer[] { 6, 3, 9, 3, 2, 4, 5, 7 };
Integer[] b = new Integer[] { 5, 8, 6, 2, 1, 9 };
List _a = Arrays.asList(a);
List _b = Arrays.asList(b);
// 创建集合
Collection realA = new ArrayList<Integer>(_a);
Collection realB = new ArrayList<Integer>(_b);
// 求交集
realA.retainAll(realB);
System.out.println("交集结果:" + realA);
Set result = new HashSet();
// 求全集
result.addAll(_a);
result.addAll(_b);
System.out.println("全集结果:" + result);
// 求差集:结果
Collection aa = new ArrayList(realA);
Collection bb = new ArrayList(result);
bb.removeAll(aa);
System.out.println("最终结果:" + bb);
/**
* 交集结果:[6, 9, 2, 5] 全集:[1, 2, 3, 4, 5, 6, 7, 8, 9] 最终结果:[1, 3, 4, 7, 8]
*/
}
}

二 查找两个有复杂对象的集合中的不同元素和相同的元素



public class RolePerm{

    private Long rid;
    private Long pid;
    public void setRid(Long rid){
        this.rid=rid;
    }
    public Long getRid(){
        return rid;
    }
    public void setPid(Long pid){
        this.pid=pid;
    }
    public Long getPid(){
        return pid;
    }
    /**
     *重写hashCode,方便集合的操作
     */
    @Override
    public int hashCode() {
        return (int) (this.pid+this.rid);
    }
    /**
     *重写equals,只有pid和rid都相等,才认为这个对象是想等的
     */
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof RolePerm)){
            return false;
        }
        RolePermrp = (RolePerm) obj;

        return (this.pid == rp.pid) && (this.rid == rp.rid);

    }

}

提取两个集合中的不同元素和相同的元素

/**
 * 获取两个集合不同
 * @param rps1  rps1数据
 * @param rps2  rps2数据
 * @return  0:rps1中独有的数据;1:交集的数据;2:rps2中的独有数据
 */
private Map<Integer, List<RolePerm>> findListDiff(List<RolePerm> rps1,List<RolePerm> rps2){
    //判断不能为空
    if(rps1 == null || rps1.isEmpty() || rps2 == null || rps1.isEmpty()) return null;
    //保存最后的数据
    Map<Integer, List<RolePerm>>  mapList = new HashMap<Integer, List<RolePerm>>(3);

    //复制rps1,作为备份
    List<RolePerm> rps1_bak = new ArrayList<RolePerm>(rps1);

    //1、获取rps1中与rps2中不同的元素
    rps1.removeAll(rps2);

    //2、获取rps1和rps2中相同的元素
    rps1_bak.removeAll(rps1);

    //3、获取rps2中与rps1中不同的元素
    rps2.removeAll(rps1_bak);

    //经过此转换后rps1中数据与rps2中的数据完全不同
    //rps1_bak是rps1和rps2的交集
    //rps2中的数据与rps1中的数据完全不同

    mapList.put(0, rps1);//rps1中独有的数据
    mapList.put(1, rps1_bak);//交集的数据
    mapList.put(2, rps2);//rps2中的独有数据


    return mapList;
}


备注:这是稍微复杂的集合,简单的集合没有这么复杂