Java集合:比较集合中的元素并在一个周期内删除

发布于 2024-09-09 09:14:18 字数 1612 浏览 3 评论 0原文

假设我有一些地理位置的集合(格式为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2024-09-16 09:14:18

您确定要在内循环中增加外层迭代器吗?

Are you sure you meant to increment your outerIterator in the inner loop?

尝蛊 2024-09-16 09:14:18

如果从外部迭代器中删除一个对象,则需要立即跳出内部循环。我不确定这是否能完全解决您的问题,但它可能会让您走得更远。

如果仍有问题,请显示错误消息和/或异常!

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!

南烟 2024-09-16 09:14:18

我认为您应该选择为您的区域使用不同的结构,例如一棵树,其中每个位置都有包含在其中的子项。这样,您可以在简单查找后排除所有与另一个区域重叠的区域。不需要嵌套迭代。

如果没有位置可以与两个位置重叠,这会更容易,情况似乎就是如此。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文