确保列表具有唯一值

时间:2022-06-30 12:45:51

How can I get a collection of unique pairs of values in Java?

如何在Java中获得一组唯一的值?

I read this other posting and tried the following code from an answer to that posting as a Pair class:

我读了这个其他帖子,并尝试从该帖子作为Pair类的回答中的以下代码:

public class Pair<L,R> {

  private final L left;
  private final R right;

  public Pair(L left, R right) {
    this.left = left;
    this.right = right;
  }

  public L getLeft() { return left; }
  public R getRight() { return right; }

  @Override
  public int hashCode() { return left.hashCode() ^ right.hashCode(); }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Pair)) return false;
    Pair pairo = (Pair) o;
    return this.left.equals(pairo.getLeft()) &&
           this.right.equals(pairo.getRight());
  }

}

But how to I ensure that a collection of Pair objects only contains unique combinations of left+right using the Pair class?

但是如何确保Pair对象的集合仅使用Pair类包含左+右的唯一组合?

Here is my first attempt:

这是我的第一次尝试:

            sel_person = this.serviceLayer.findSelPerson(patntId);
            ArrayList<Pair> ps = new ArrayList<Pair>();
            for(int ee=0;ee<sel_person.getRoles().size();ee++){
                Pair p1 = new Pair(sel_person.getRoles().get(ee).getId1Root(), sel_person.getRoles().get(ee).getId1Extension());
                ps.add(p1);
                Pair p2 = new Pair(sel_person.getRoles().get(ee).getId2Root(), sel_person.getRoles().get(ee).getId2Extension());
                ps.add(p2);
            }

My fear is that the ArrayList will not care if there are redundant combinations of left+right. I wonder if the hashcode method will somehow prevent instantiation of Pair objects with redundant combinations of left+right`, but how can I be sure? If the above code is correct, then a good answer would simply explain why. If something else needs to be done, then a good answer would explain what else needs to be done.

我担心ArrayList不关心左右+是否存在冗余组合。我想知道hashcode方法是否会以某种方式阻止具有left + right`冗余组合的Pair对象的实例化,但我怎么能确定?如果上面的代码是正确的,那么一个好的答案就可以解释原因。如果还需要做其他事情,那么一个好的答案将解释还有什么需要做。

2 个解决方案

#1


But how to I ensure that a collection of Pair objects only contains unique combinations of left+right using the Pair class?

但是如何确保Pair对象的集合仅使用Pair类包含左+右的唯一组合?

A List cares not about duplicate elements. An implementation of the Set interface (for instance HashSet) is more appropriate, as a Set is defined as a Collection of unique elements (uniqueness as defined by hashCode and equals of the Objects it contains).

List不关心重复元素。 Set接口(例如HashSet)的实现更合适,因为Set被定义为唯一元素的集合(hashCode定义的唯一性和它包含的Objects的equals)。

Set<Pair> ps = new HashSet<Pair>();
for(int ee=0;ee<sel_person.getRoles().size();ee++){
    Pair p1 = new Pair(sel_person.getRoles().get(ee).getId1Root(), sel_person.getRoles().get(ee).getId1Extension());
    ps.add(p1);
    Pair p2 = new Pair(sel_person.getRoles().get(ee).getId2Root(), sel_person.getRoles().get(ee).getId2Extension());
    ps.add(p2);
}
//now ps has only unique pairs as defined by the Pair class hashcode/equals

#2


HashSet does the trick for you

HashSet为您提供了诀窍

ArrayList<Pair> ps = new ArrayList<Pair>();
Set<Pair> set = new HashSet<Pair>(ps);

set will then contain the unique objects.

然后,set将包含唯一对象。

#1


But how to I ensure that a collection of Pair objects only contains unique combinations of left+right using the Pair class?

但是如何确保Pair对象的集合仅使用Pair类包含左+右的唯一组合?

A List cares not about duplicate elements. An implementation of the Set interface (for instance HashSet) is more appropriate, as a Set is defined as a Collection of unique elements (uniqueness as defined by hashCode and equals of the Objects it contains).

List不关心重复元素。 Set接口(例如HashSet)的实现更合适,因为Set被定义为唯一元素的集合(hashCode定义的唯一性和它包含的Objects的equals)。

Set<Pair> ps = new HashSet<Pair>();
for(int ee=0;ee<sel_person.getRoles().size();ee++){
    Pair p1 = new Pair(sel_person.getRoles().get(ee).getId1Root(), sel_person.getRoles().get(ee).getId1Extension());
    ps.add(p1);
    Pair p2 = new Pair(sel_person.getRoles().get(ee).getId2Root(), sel_person.getRoles().get(ee).getId2Extension());
    ps.add(p2);
}
//now ps has only unique pairs as defined by the Pair class hashcode/equals

#2


HashSet does the trick for you

HashSet为您提供了诀窍

ArrayList<Pair> ps = new ArrayList<Pair>();
Set<Pair> set = new HashSet<Pair>(ps);

set will then contain the unique objects.

然后,set将包含唯一对象。