开发者

How to add an object into another Object set

开发者 https://www.devze.com 2023-01-12 11:56 出处:网络
I have two classes. One(Person) for getters and setters, and another(People) for compute the data. What is my situation is, I get the data from the DB using ResultSet, then createda person Object to s

I have two classes. One(Person) for getters and setters, and another(People) for compute the data. What is my situation is, I get the data from the DB using ResultSet, then created a person Object to store the row data. Then i created people Object to store all persons.

Each Object created as 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;

Now the problem is the last line in the While Loop people.add(person);

It says

The method add(People) in the type Set is not applicable for the arguments (Per开发者_JAVA百科son)

How can i overcome this problem?

Thanks.


My understandig from your design is that you have a People has-many Person relation, so the People class holds a collection of Person objects. Then I'd expect something like this:

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, ...
}

So in your database related code you wouldn't need to create another set, because People already has the one you need:

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;


I don't understand why you would want 2 classes in the first place. You cand have Person implement the computational part as well. But, nevertheless, what you could do:

class People implements Set<Person> {

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

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

and then:

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;


I understand Person is a data structure (bean-like, with getters and setters), and People should contain all Person objects from the database and perform calculations on them.

If that is true, first of all, you cannot declare people within the loop (because a new People object will be created for each Person, and you don't want that, from what I understand).

Second, People needs to be able to contain Person objects. So it should at least be composed of a Set of Person objects. You can add more functionality as you please. So, try something like this:

public class People {

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

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

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

}

And use it like this, as the previous poster suggested:

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();


Based on what you are trying to do I feel, you should convert your Person to People class before adding to the set. Your People class may have a constructor which takes a Person as an argument and copies the required fields from Person to People. Here your code to add to set will look like people.add(new People(person));

When you declare Set<People> people = new HashSet<People>(); what it means is that this set is supposed to contain objects of 'type' People i.e. instances of People or instances of subclasses of People. If People is an interface, then the set may contain any object that implements the interface.


I don't think that Set<People> people = new HashSet<people>(); should be written in the loop.


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();
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消