如何将一个对象添加到另一个对象集合中

4
我有两个类。一个是用于获取和设置属性的Person类,另一个是用于计算数据的People类。我的情况是,我使用ResultSet从数据库中获取数据,然后创建一个Person对象来存储行数据。接着我创建了一个People对象来存储所有的Person对象。
每个对象都是SET方式创建的。
while(rs.next())
{
    Set<People> people = new HashSet<people>();
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

现在的问题在于 While 循环的最后一行 people.add(person);

它显示:

类型 Set 中的方法 add(People) 对于参数 (Person) 不适用

我该如何解决这个问题?

谢谢。

6个回答

10

根据您的设计,我的理解是您有一个People has-many Person的关系,因此People类包含了一组Person对象。 那么我期望看到类似以下的代码:

public class Person {
  private String name;
  private Date dateOfBirth;
  // .. more attributes

  // getters and setters

  // overrides of equals, hashcode and toString
}

public class People implements Set<Person> {
  private Set<Person> persons = new HashSet<Person>();

  public boolean add(Person person) {
    return persons.add(person);
  }

  // more methods for remove, contains, ...
}

因此,在您的数据库相关代码中,您不需要创建另一组,因为 People 已经有了您所需的那个组:

People people = new People();  // or get it, if it's already created
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

2

我不明白为什么你要在第一次使用时就创建2个类。你可以让Person类实现计算部分。但是,无论如何,你可以这样做:

class People implements Set<Person> {

private HashSet<Person> hashset = new HashSet<Person>();

// ... your computational code goes here
// delegate all Set methods to hashset
}

接着:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

同意。如果人们使用HashMap代替继承,会更好:) - 卢声远 Shengyuan Lu
正确。不过你也可以推理出来 :) - drstupid

2

我理解Person是一个数据结构(类似bean,带有getter和setter),而People应该包含来自数据库的所有Person对象并对它们执行计算。

如果这是真的,首先,你不能在循环内声明people(因为每个Person都会创建一个新的People对象,而你不希望这样,根据我的理解)。

其次,People需要能够包含Person对象。所以它至少应该由一组Person对象的Set组成。你可以根据需要添加更多功能。所以,试试这样:

public class People {

    Set<Person> persons = new HashSet<Person>();

    Set<Person> getPersons() {
        return persons;
    }

    int computeSomethingAboutPeople() { 
        // return as you please
    }

}

像前面的帖子建议的那样,可以这样使用:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.getPersons().add(person);
}
int answer = people.computeSomethingAboutPeople();

1
有一个实现了 Set 接口的 People。然后你可以将其用作 Set,而不必强制用户执行类似于 people.getPersons().add(person); 的操作。如果为集合提供一个 getter 方法,请不要返回内部集合,而是返回 Collections.unmodifiableSet(persons),这样就不会有人改变内部数据。 - Andreas Dolk
谢谢你的回答。它对我很有帮助。但是我还有另一个问题。我是否需要为Person类声明一个集合,例如:Set<Person> persons = new HashSet<Person>();。 - user405398

1

根据你想要做的事情,我觉得在将Person添加到集合之前,你应该将其转换为People类。你的People类可以有一个构造函数,它接受一个Person作为参数,并将所需的字段从Person复制到People。这样,你添加到集合的代码将如下所示:people.add(new People(person));

当你声明Set<People> people = new HashSet<People>();时,它意味着这个集合应该包含'type'为People的对象,即People的实例或People的子类的实例。如果People是一个接口,那么集合可以包含任何实现该接口的对象。


0
class Cartesian
{
   double x,y,z;
   public
   Cartesian()
   {
      x=y=z=0;
   }
   Cartesian (int i,int j,int k)
   {
        x=i;
        y=j;
        z=k;
   }

   Cartesian add_coordinates(Cartesian c)
   {
        c.x=x+c.x;
        c.y=y+c.y;
        c.z=z+c.z;      
        return c;                 
   }

   void display()
   {
        System.out.println("Addition of coordinates is : "+x+"i "+y+"j "+z+"k ");
   }

}

class Coordinate
{
  public static void main(String[] args)
  {
       Cartesian obj1 = new Cartesian(5,5,-10);
       Cartesian obj2 = new Cartesian(5,5,-10);
       Cartesian obj3 = new Cartesian();
       obj3=obj1.add_coordinates(obj2);
       obj3.display();
  }
}

请考虑添加一些解释。 - Sunil

0

我认为在循环中不应该写成 Set<People> people = new HashSet<people>();


1
大问题在于“people.add(person);”这一行。声明只是一个打字错误。 - drstupid
是的,在那里我只是提到它了...... 它实际上没有在那里声明。 - user405398

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接