Java Collection在遍历中删除、合并元素


目录

我的分词结果链表需要合并连续的数字和日期,所以需要熟悉一下Java Collection在遍历的过程中同时删除、合并元素的小trick。自己试验了一下,活用listIterator的previous()和next()方法就可以达到目的。

遍历中删除

数据集


   
   
  1.         List<Integer> integerList = new LinkedList<Integer>();
  2.         for (int i = 1; i <= 10; ++i)
  3.         {
  4.             integerList.add(i);
  5.         }
  6.         System.out.println(integerList);

也就是

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

遍历中删除所有奇数


   
   
  1. // 遍历中删除所有奇数
  2.         ListIterator<Integer> listIterator = integerList.listIterator();
  3.         while (listIterator.hasNext())
  4.         {
  5.             Integer next = listIterator.next();
  6.             if (next % 2 == 1)
  7.             {
  8.                 listIterator.remove();
  9.             }
  10.         }
  11.         System.out.println(integerList);

输出:

[2, 4, 6, 8, 10]

遍历中合并

数据集

数据集接上面的输出

遍历中合并前后和为10的项


   
   
  1. // 遍历中合并前后和为10的项
  2.         listIterator = integerList.listIterator();
  3.         Integer next = listIterator.next();
  4.         while (listIterator.hasNext())
  5.         {
  6.             Integer current = next;
  7.             next = listIterator.next();
  8.             if (current + next == 10)
  9.             {
  10.                 // 此时listIterator指向6
  11.                 listIterator.remove();
  12.                 // 删除后listIterator还是指向6,但是其元素是无效的
  13.                 listIterator.previous();
  14.                 // 回退到4,将其合并为10
  15.                 listIterator.set(10);
  16.                 // 接下来会next到8
  17.             }
  18.         }
  19.         System.out.println(integerList);

输出:

[2, 10, 8, 10]

数据集


   
   
  1.  // 遍历中合并前后相等的项
  2.         integerList.clear();
  3.         for (int i = 1; i <= 5; ++i)
  4.         {
  5.             for (int j = 0; j < 3; ++j)
  6.             {
  7.                 integerList.add(i);
  8.             }
  9.         }
  10.         System.out.println(integerList);

也就是

[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5]

遍历中合并前后相等的项


   
   
  1.  listIterator = integerList.listIterator();
  2.         next = listIterator.next();
  3.         while (listIterator.hasNext())
  4.         {
  5.             Integer current = next;
  6.             next = listIterator.next();
  7.             if (current == next)
  8.             {
  9.                 // 此时listIterator指向6
  10.                 listIterator.remove();
  11.                 // 删除后listIterator还是指向6,但是其元素是无效的
  12.                 listIterator.previous();
  13.                 // 回退到4,将其合并为10
  14.                 listIterator.set(current + next);
  15.                 // 接下来会next到8
  16.             }
  17.         }
  18.  
  19.         System.out.println(integerList);

输出

[2, 1, 4, 2, 6, 3, 8, 4, 10, 5]

值得注意的是,上面并没有合并所有相同的项,因为在一句listIterator.set(current + next);之后,前后就不相等了。

本来想用两个迭代器去做,但是一个迭代器删除之后,会导致另一个迭代器失效,抛出ConcurrentModificationException异常,所以只能操作单个迭代器。

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » Java Collection在遍历中删除、合并元素

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值