高效筛选两个List中的不同的元素
来源:互联网 发布:金十数据官网财经日历 编辑:程序博客网 时间:2024/05/14 02:27
问题记录:
开发过程中,需要把两个List中不同的元素筛选出来,这两个List的数据量都很大,如果按照一般的方法,分别去遍历两个List,然后分别对每一个元素做比较,时间消耗将会达到m*n,处理效率显然不尽人意。
解决思路:
使用一个Map来对2个List中的元素进行计数:
即把List的元素作为Map的Key,Entry的Value为Integer类型,用于记录元素在两个集合中出现的次数。
解决方案:
先遍历一个List中的所有元素,put进Map,初始出现次数为1;
再遍历第二个List中的所有元素,与map已有的元素进行比较:
如果Map中不存在这个元素,就把这个元素插入结果集,
如果Map中存在这个元素,则把这个元素的出现次数置为2。
代码示例:
示例实体类Product:
public class Product {private Integer id;private String name;public Product(Integer id, String name) {this.id = id;this.name = name;}public Integer getId() {return id;}public String getName() {return name;}@Overridepublic String toString() {return "Product [id=" + id + ", name=" + name + "]";}public boolean equals(Object o){if (o == null) {return false;}if (this == o) {return true;}if (o instanceof Product) {Product p = (Product) o;if (p.getId() == this.getId() && p.getName().equals(this.getName())) {return true;}else {return false;}}return false;}public int hashCode(){int result = 17;result = result*37 + id;result = result*37 + name.hashCode();return result;}}示例解决方法:
public static Collection<Product> getDiffrent(Collection<Product> col1, Collection<Product> col2){//创建返回结果Collection<Product> diffrentResult = new ArrayList<>();//比较出两个集合的大小,在添加进map的时候先遍历较大集合,这样子可以减少没必要的判断Collection<Product> bigCol = null;Collection<Product> smallCol = null;if (col1.size() > col2.size()) {bigCol = col1;smallCol = col2;}else {bigCol = col2;smallCol = col1;}//创建 Map<对象,出现次数> (直接指定大小减少空间浪费)Map<Object, Integer> map = new HashMap<>(bigCol.size());//遍历大集合把元素put进map,初始出现次数为1for(Product p : bigCol) {map.put(p, 1);}//遍历小集合,如果map中不存在小集合中的元素,就添加到返回结果,如果存在,把出现次数置为2for(Product p : smallCol) {if (map.get(p) == null) {diffrentResult.add(p);}else {map.put(p, 2);}}//把出现次数为1的 Key:Value 捞出,并把Key添加到返回结果for(Map.Entry<Object, Integer> entry : map.entrySet()) {if (entry.getValue() == 1) {diffrentResult.add((Product) entry.getKey());}}return diffrentResult;}测试代码:
public static void main(String[] args) {List<Product> list1 = new ArrayList<>();List<Product> list2 = new ArrayList<>();for (int i = 0; i < 10; i++) {list1.add(new Product(i, "Product"+String.valueOf(i)));}for (int i = 0; i < 10; i = i + 2) {list2.add(new Product(i, "Product"+String.valueOf(i)));}Collection<Product> result = getDiffrent(list1, list2);for(Product p : result) {System.out.println(p.toString());}}测试结果:
Product [id=7, name=Product7]Product [id=1, name=Product1]Product [id=9, name=Product9]Product [id=3, name=Product3]Product [id=5, name=Product5]
解决过程中遇到的问题:
由于是把自定义类作为Map的Key,势必会存在一个问题:
Map在get的时候,是没有办法直接get到这个Key对应的键值对的。
解决办法:
由HashMap的get方法的源码:
public V get(Object key) { if (key == null) return getForNullKey(); Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue(); }
再来看看getEntry方法的源码:
final Entry<K,V> getEntry(Object key) { if (size == 0) { return null; } int hash = (key == null) ? 0 : hash(key); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null; }
由源码可以看出,HashMap在根据Key查找的时候,是根据hashCode的值和equals方法来查找这个Key所对应的键值对的,显然我们需要重写自定义类的equals()方法和hashCode()方法。
由于这里只需要判断对象的逻辑相等,重写的equals()方法只需要判断各个属性值是否相等即可
public boolean equals(Object o){if (o == null) {return false;}if (this == o) {return true;}if (o instanceof Product) {Product p = (Product) o;if (p.getId() == this.getId() && p.getName().equals(this.getName())) {return true;}else {return false;}}return false;}
重写hashCode()方法
学习了《Effective Java》中提出的一种简单通用的hashCode算法
1. 初始化一个整形变量,为此变量赋予一个非零的常数值,比如int result = 17;
2. 选取equals方法中用于比较的所有域,然后针对每个域的属性进行计算:
(1) 如果是boolean值,则计算f ? 1:0
(2) 如果是byte\char\short\int,则计算(int)f
(3) 如果是long值,则计算(int)(f ^ (f >>> 32))
(4) 如果是float值,则计算Float.floatToIntBits(f)
(5) 如果是double值,则计算Double.doubleToLongBits(f),然后返回的结果是long,再用规则(3)去处理
long得到int
(6) 如果是对象应用,如果equals方法中采取递归调用的比较方式,那么hashCode中同样采取递归调用
hashCode的方式。否则需要为这个域计算一个范式,比如当这个域的值为null的时候,那么hashCode值为0。
(7) 如果是数组,那么需要为每个元素当做单独的域来处理。如果你使用的是1.5及以上版本的JDK,那么没
必要自己去重新遍历一遍数组,java.util.Arrays.hashCode方法包含了8种基本类型数组和引用数组的
hashCode计算,算法同上。
public int hashCode(){int result = 17;if (id != null) {result = result*37 + id;}if (name != null) {result = result*37 + name.hashCode();}return result;}
- 高效筛选两个List中的不同的元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效的找出两个List中的不同元素
- 高效找出两个List中不同的元素
- 关于两个List如何快速高效去除相同的元素,保留不同的元素
- 快速计算两个List的不同元素
- 找出list中的不同元素、删除两个list中相同的对象
- 多数据情况下 高效性的 找到两个List中的相同,不同等数据信息
- Java中找到两个list中的不同元素
- 有关《查找两个List中的不同元素》的问题解答与编程实践
- 比较两个List元素是否相同,查找出两个list的不同元素
- 两个List对比筛选
- PHP-查询两个数组中的不同元素
- 数据库的一些概念
- TF-IDF与余弦相似性的应用(一):自动提取关键词
- 使用Httpclient(post请求)上传文件及其他参数到https协议的服务器
- volatile关键字总结
- Linux下远程登录阿里云服务器
- 高效筛选两个List中的不同的元素
- 简单对比Flat, Gouraud & Phong shading
- 为什么在浏览器的地址栏上输入 url 就能访问到 Web 网页
- xgboost on yarn 安装
- css 水平居中 垂直居中 水平垂直居中
- Python爬虫之静态页面爬取
- 面试题之“约德尔测试”
- ES6-数组的扩展-数组的空位
- MyBatis PageHelper插件使用