【问题标题】:Unique Sets from collection of Sets来自集合集合的唯一集合
【发布时间】:2012-08-27 09:51:07
【问题描述】:

我确实有 int 对,即; (int,int)

1) 给定 k 个这样的对,检查它们是否唯一。 IE;使用 k 对形成的 Set 的大小为 k ?
2) 如果给定的 k 记录是唯一的,则按排序顺序存储它们(按 x 并按 y 解决冲突)
3) 给定 n 个大小为 k 的集合,创建一个集合。

要求 1 和 2 的示例
如果 k = 3

(100, 100) (110, 300) (120, 200) 是一个有效的集合并按排序顺序。
(100, 100) (300, 200) (200, 300) 是有效集合,但不是按排序顺序。
(100, 100) (100, 200) (100, 200) 在有效集合中

要求 3 的示例
输入:

(100, 100) (200, 300) (300, 200)
(100, 100) (200, 300) (300, 200)
(100, 100) (201, 300) (300, 200)

输出:

(100, 100) (200, 300) (300, 200)
(100, 100) (201, 300) (300, 200)

这是与我面临的真正问题最接近的类比。我需要用 Java 完成这项工作,而我从未在 Java 中工作过。我是一名中级 c++ 程序员。

我可以通过一些丑陋的编码和排序来解决 1 和 2。
但是我无法获得 3。以下是到目前为止我可以获得的 3。类对实现了可比较的

(poc 码)

import java.util.HashSet;
public class set {
    public static void main (String []args) {
        HashSet<Pair> s1 = new HashSet();
        s1.add(new Pair(10,10));
        s1.add(new Pair(10,10));

        HashSet<Pair> s2 = new HashSet();
        s2.add(new Pair(10,10));
        s2.add(new Pair(10,10));

        HashSet<HashSet<Pair>> s12 = new HashSet();
        s12.add(s1);s12.add(s2);
        for ( HashSet<Pair> hs : s12) {
            for (Pair p :  hs) {
                System.out.println(""+ p.toString());
            }
        }
    }
}

【问题讨论】:

  • 是和否。我开始通过作业问题探索计算几何算法。在移动线扫描和其他算法之前,我想自己尝试一下en.wikipedia.org/wiki/Line_segment_intersection
  • 你的 HashSet 代码到底有什么问题?看起来没问题,进行一些未经检查的操作。
  • First HashSet 不识别重复项。如果我必须检查 (10,10)(10,10) 使一组大小为 2 的对具有唯一对,那么上面编写的代码不起作用。理想情况下,s1 在插入该对 2 次后应该只有 (10,10)。我需要知道如何使用自定义 compare()/equal 使其成为排序集 类似的是 HashSet> 的问题,它应该是唯一 HashSet 的集合 上述代码的输出应该是( 10, 10) ( 忽略 k 必须是 2) 但是它正在打印 (10, 10) (10, 10) (10, 10) (10, 10)
  • Pair 是如何定义的?具体来说,它是否正确实现了equalshashCode
  • pair 实现了可比性。我还没有弄清楚如何让它实现两者/全部。 IE;可比,等于,哈希码。 (基本上实现多个接口。在 C++ 中,我会做多重继承,不确定 java 语法)我主要关心的是,我采用的方法是否有效?有没有更好的收藏品用于此目的?

标签: java algorithm collections set


【解决方案1】:

看起来您没有覆盖 Pair 类中的 equals 和/或 hashCode 方法。

例如,如果您的 Pair 类具有以下结构:

protected K value1;
protected V value2; 

您应该将equalshashCode 实现为(示例):

 public boolean equals(Object obj) {
    if (!(obj instanceof Pair))
        return false;
    Pair that = (Pair)obj;
    boolean result = true;
    if (this.getValue1() != null)
        result = this.getValue1().equals(that.getValue1());
    else if (that.getValue1() != null)
        result = that.getValue1().equals(this.getValue1());

    if (this.getValue2() != null)
        result = result && this.getValue2().equals(that.getValue2());
    else if (that.getValue2() != null)
        result = result && that.getValue2().equals(this.getValue2());

    return result;
} 


public int hashCode() {
    int result = value1 != null ? value1.hashCode() : 0;
    result = 31 * result + (value2 != null ? value2.hashCode() : 0);
    return result;
} 

【讨论】:

  • 看起来这就是我所缺少的。但总的来说,有没有更好的方法来处理问题?
  • 我认为更好的方法是使用 TreeSet 而不是 HashSet,因为您希望按排序顺序存储配对对象。在任何情况下,您都必须实现 equals 和 hashCode..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多