Java集合:比较集合中的元素并在一个周期内删除
假设我有一些地理位置的集合(格式为 Country > Region [ > Town [ > District]]
),并且我想删除彼此重叠的位置(例如,欧洲 > 德国
与德国 > 德累斯顿
和欧洲 > 德国 > 汉堡
重叠,因此必须删除最后两个。我发现我需要两个迭代器实例才能实现如下所示:
final Iterator<Location> outerIterator = locations.newIterator();
while (outerIterator.hasNext()) {
final Location outer = outerIterator.next();
final Iterator<Location> innerIterator = locations.newIterator();
while (innerIterator.hasNext()) {
final Location inner = innerIterator.next();
if (!inner.equals(outer)) {
if (inner.overlaps(outer)) outerIterator.remove();
else if (outer.overlaps(inner)) innerIterator.remove();
}
}
}
但我无法为同一个集合获取新的 Iterator
。我的算法不正确还是有办法做到正确?
使用 Carl Smotricz 提供的答案如下所示:
final Iterator<JobLocation> outerIterator = locations.iterator();
while (outerIterator.hasNext()) {
final JobLocation outer = outerIterator.next();
final Iterator<JobLocation> innerIterator = locations.iterator();
while (innerIterator.hasNext()) {
final JobLocation inner = innerIterator.next();
if (!inner.equals(outer) && inner.overlaps(outer)) {
outerIterator.remove();
break;
}
}
}
Say, I have a collection of some geolocations (in format Country > Region [ > Town [ > District]]
) and I want to remove locations that overlap each other (for example, Europe > Germany
overlaps Europe > Germany > Dresden
and Europe > Germany > Hamburg
, so the last two must be removed). I see that I need two instances of iterators to make something like this:
final Iterator<Location> outerIterator = locations.newIterator();
while (outerIterator.hasNext()) {
final Location outer = outerIterator.next();
final Iterator<Location> innerIterator = locations.newIterator();
while (innerIterator.hasNext()) {
final Location inner = innerIterator.next();
if (!inner.equals(outer)) {
if (inner.overlaps(outer)) outerIterator.remove();
else if (outer.overlaps(inner)) innerIterator.remove();
}
}
}
But I can't get new Iterator
for the same collection. Is my algorithm incorrect or there is a way to do it right?
The final code using the advice from the answer provided by Carl Smotricz looks like this:
final Iterator<JobLocation> outerIterator = locations.iterator();
while (outerIterator.hasNext()) {
final JobLocation outer = outerIterator.next();
final Iterator<JobLocation> innerIterator = locations.iterator();
while (innerIterator.hasNext()) {
final JobLocation inner = innerIterator.next();
if (!inner.equals(outer) && inner.overlaps(outer)) {
outerIterator.remove();
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定要在内循环中增加外层迭代器吗?
Are you sure you meant to increment your outerIterator in the inner loop?
如果从外部迭代器中删除一个对象,则需要立即跳出内部循环。我不确定这是否能完全解决您的问题,但它可能会让您走得更远。
如果仍有问题,请显示错误消息和/或异常!
If you remove an object from the outer iterator, you need to break out of the inner loop immediately afterwards. I'm not sure if that will solve your problem completely, but it may get you a bit further along.
If there's still a problem, please show the error message and/or exception!
我认为您应该选择为您的区域使用不同的结构,例如一棵树,其中每个位置都有包含在其中的子项。这样,您可以在简单查找后排除所有与另一个区域重叠的区域。不需要嵌套迭代。
如果没有位置可以与两个位置重叠,这会更容易,情况似乎就是如此。
I think you should opt to use a different structure for your regions, like a tree where each location has children that are contained by it. This way you can exclude all regions that overlap another after a simple look-up. No nested iterations needed.
This is easier if no location can overlap with two locations, which seems to be the case.