How to add an object to another set of objects

I have two classes. One (Face) for getters and setters, and the other (People) for calculating data. What is my situation, I get data from the database using ResultSet, and then created a human Object to store the row data. Then I created Object people to store all faces.

Each object is created as a 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 While Loop people.add(person);

It says

The add (People) method in the Set type is not applicable for arguments (Person)

How can I solve this problem?

Thank.

+3
source share
6 answers

, , 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 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;
+8

, 2 . , . , , :

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;
+2

, , , Person to People. " " , " ". people.add(new People(person));

Set<People> people = new HashSet<People>();, , "", , "". People - , , .

+1

, Person - (bean -like, ), People Person .

, , ( Person People, , ).

-, People Person. Set Person. , . , - :

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> people = new HashSet<people>(); .

0
source
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
source

Source: https://habr.com/ru/post/1761058/


All Articles