【问题标题】:Count duplicate Integer pairs in an arrayList in java在java中的arrayList中计算重复的整数对
【发布时间】:2014-09-18 19:43:20
【问题描述】:

我有一个算法要运行,在其中一个步骤中,我必须在名为 (PairList) 的列表中找到重复的对,计算它们并消除小于特定参数 (minSupp) 的对。这是我的代码用于将对添加到 PairList。

for (int i = sizes.get(sequence.getId())-maxError ; i <= sizes.get(sequence.getId())-1; i++){

             for(int j = i+1; j<sizes.get(sequence.getId()); j++){
                 //Get the first Item 
                int first = sequence.getItemsets().get(i);
                //gets the second Item
                int second =sequence.getItemsets().get(j);
                //Generate pattern as pair
                Pair pattern  = new Pair(first, second);

                sequenceID = sequence.getId();
                //Generate triples with sequence ID and pair
                Triples triples = new Triples(sequenceID, i, j);
                if (!pairList.contains(pattern)){
                    //adds if it doesn't exist
                    pairList.add(pattern);
                }

现在 pairList 包含一些像这样的对: (3, 28) (3, 58) (3, 61) (3, 28) (5, 21) (3, 28) (5, 21) 例如,我想知道此列表中出现了多少次 (3, 28)。对于 (minSupp=2) 我想删除出现少于 2 次的对所以输出应该是这样的:

   (3, 28) : 3 
   (3, 58) : 1 (this must be removed)
   (3, 61) : 1 remove
   (5, 21) : 2 

我一直在研究它,这是我到目前为止的代码,但它给我的输出与我想要的相差太多,所以请帮忙!

  for(Pair pair : pairList){
                    int a = Collections.frequency(pairList, pair);

                    for (int i=0 ; i<pairList.size() ; i++){
                        for (int j =i+1 ; j<pairList.size()-1;j++){

                        if (pairList.get(i).getX()==pairList.get(j).getX() && pairList.get(i).getY()==pairList.get(j).getY() ){
                         a++;
                        System.out.println(pair + ": " + a);
                        } 

【问题讨论】:

  • 覆盖Pair类的hashCodeequals方法,并使用Collections.frequency()

标签: java arraylist count duplicates


【解决方案1】:
如果您在 Pair 类中实现了equals

Collections.frequency() 已经足以在列表中找到一对的频率。因此,您的嵌套 for 循环是多余的。 可以通过.remove() 简单地删除这些对。如果您希望每对只打印一次,您可以将列表添加到 Set 中,然后对其进行迭代(您还需要实现 hashCode):

for (Pair pair : new HashSet<Pair>(pairList)) { // a set has no double entries
    int frequency = Collections.frequency(pairList, pair);
    System.out.print(pair + " : " + frequency); // print frequency of pair

    if (frequency < minSupp) { // if frequency is too low remove this pair
        while (pairList.remove(pair)); // remove all occurrences of this pair
        System.out.print(" remove");
    }

    System.out.println();
}

有关如何正确实施hashCodeequals 的更多信息,请查看以下问题:What issues should be considered when overriding equals and hashCode in Java?

【讨论】:

  • 我在我的 Pair 类中实现了 equals,当我尝试你的代码时,它给了我这个输出 (3,28) :1 remove (3,28) :1 remove .... 所以它赢了'不计算频率,否则它不会给我 (3, 28) :3,因为之后我必须再次使用该数字。
  • 您确定您已正确实施equals 吗?建议的代码对我有用。我忘记的是,为了让 HashSet 正常工作,您还需要覆盖 hashCode。我添加了一个链接,该链接很好地解释了如何正确覆盖这两种方法。
  • 为了快速测试它,我使用了:return x + y; // could have collisions! 用于hashCodePair that = (Pair) y; return this.x == that.x &amp;&amp; this.y == that.y; 用于equals。 (注意:您应该只使用该实现进行快速测试,然后按照我添加的链接正确实现这些方法。)
  • 这是我的对等课程。我不知道这有什么问题? public boolean equals(Pair pair){ boolean status = false; if(this.getX() != pair.getX()){ status = true; } if(this.getY() != pair.getY()){ status = true; } System.out.println(状态);返回状态; } @Override public String toString(){ return "("+x+", "+y+")"; }
  • equals 必须有一个 Object 作为参数 -> public boolean equals(Object obj)。然后,您必须在进行属性比较之前将 Object 转换为 Pair。有关详细信息,请参阅我添加到答案中的链接。
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多