[英]find common elements in two lists
我有两个类型为CRVariable的数组列表(休眠类),我想找到它们的公共元素(我猜是交叉点)。
这是我的CRVariable休眠类:
@Entity
@Table(name = "imageviewer_crvariable")
public class CRVariable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "VarId")
private Long varId;
@Column(name = "VarName", unique=true)
private String varName;
@Column(name = "VarDescription")
private String varDescription;
private String state;
@ManyToMany(mappedBy="crvariables")
private Set<CRImageType> crimagetypes = new HashSet<CRImageType>();
@OneToMany(mappedBy="cRVariable")
private Set<CRFormField> cRFormFields;
....
因此,我尝试通过以下方式使用Collection#retainAll()方法:
...
List<CRVariable> variablesForCurrentImageType = new ArrayList<CRVariable>();
variablesForCurrentImageType = getCRVariablesForImageType(mySelectedImages.get(k));
...
List<CRVariable> mySelectedVariables = Arrays.asList(selectedVariables);
...
Collection<CRVariable> colA1 = new ArrayList<CRVariable>();
colA1.addAll(mySelectedVariables);
Collection<CRVariable> colB1 = new ArrayList<CRVariable>();
colB1.addAll(variablesForCurrentImageType);
...
if(colA1.size()>colB1.size()){
colA1.retainAll(colB1); //intersection
System.out.println(">>>> INTERSECTION SIZE: " + colA1.size());
} else {
colB1.retainAll(colA1); //intersection
System.out.println(">>>> INTERSECTION SIZE: " + colB1.size());
}
...
但是,无论哪种组合,如果我尝试我总是会得到零(0)的colA1或colB1大小!
关于我所缺少的任何想法?
您可能没有正确实现equals()。
keepAll()方法无法自动知道哪些对象相等,因此您必须提供该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.