求求 关于JAVA里面合并两个collection<T>的问题求帮助啊啊啊

我是个新手 完全不太会java 求大神帮助我下 问题是这样的我有两个database 然后我把他们的table读出来写在collection
的class里面 class T { private final String T; public Rfq(String T) { this.T= T; } public String getTId() { return TId; } } 大概是这样 其实我也不是很懂 然后我连接两个database得到的是两个resultA和resultB 大概code是 collection
resultA= ...connection.query (select from ... ) 我现在的问题是我要把这两个result里面同样column name的(比如 两个都有个column叫id) 我要找到一个合集 求帮助阿求帮助 求指点!!

首先T类中增加hashCode和equals方法。

public class T {
  private String id;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    T other = (T) obj;
    if (id == null) {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    return true;
  }
}

然后,得到resultA和resultB后,

Collection<T> resultA = ...;
Collection<T> resultB = ...;

resultA.retainAll(resultB);// 执行完这句后,resultA即为从两边数据库取出来的id相同的记录

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答