Using a method from one class to a set

I am having problems getting a method from one class to work if I put objects in a set.

So, I have a

public class Employee { /* instance variables */ private String firstName; private String employeeNumber; public Employee(String employNum) { super(); this.employeeNumber = employNum; } 

...

 public String getFirstName() { return this.firstName; } 

There is a lot of other code that I can send if necessary, but I am not allowed to modify the Employee class.

So, for my code, I need to create a class for Set from Employee , which I did with

 public class Records { public Set<Employee> employeeSet = new HashSet<Employee>(); public Records() { } } 

Now I need a method that will print the details of all the employees in the set. Here is my attempt so far

 public void printEmployeeNames() { for (String employee : employeeSet) { System.out.println(this.employeeSet.getFirstName()); } } 

The problem I am facing is that it will not compile as it says

"incompatible types"

and highlights employeeSet in

 for (String employee : employeeSet) 

Another problem is that it cannot access the getFirstName() method. I tried to isolate the method using

 public void printEmployeeNames() { System.out.println(this.employeeSet.getFirstName()); } 

It will also not compile as it indicates

"cannot find character - getFirstName () method".

Change Thanks for the help in solving this problem, I changed it to this, and it worked.

 public void printEmployees() { for (Employee employee: employeeSet) { System.out.println(employee.getFirstName()); } } 
+5
source share
3 answers

this doesn't make sense here:

 for (String employee: employeeSet) { System.out.println(this.employeeSet.getFirstName()); } 

since employeeSet is Set and set does not have a method named getFirstName

you need to do:

 for (Employee employee: employeeSet) //for every EMPLOYEE in the employeeSet { System.out.println(employee.getFirstName()); //from that employ get the name } 

And create the corresponding setter and recipients in the Employee class

in this case:

 private String firstName; /** * @return the employeeNumber */ public final String getEmployeeNumber() { return firstName; } 
+2
source

It should be

 for (Employee employee: employeeSet) { System.out.println(employee.getFirstName()); } 

Set does not have a firstname method. Your employee object has.

+1
source

First of all, have you heard of encapsulation ? The public Set<Employee> employeeSet declaration is an example of bad practice, and you should use a private field with some kind of getter. The reason your for loop causes errors is because you made two errors:

  • employeeSet is a List<Employee> , while you ask String when iterating over it. This is not true - change the employee type to employee .

  • You are trying to access getFirstName() from your employeeSet field. This will not work since Set does not have such a method. I suppose you wanted to call a method on employee .

In addition, you can simplify your code for the following single-line interface with Java 8 threads :

 public void printEmployeeNames() { employeeSet.stream().map(Employee::getFirstName).forEach(System.out::println); } 
+1
source

All Articles