Rawsick
Rawsick

Reputation: 19

Putting an array of objects into a Map and the Set

In EmployeeSearch’s main method, create an array of several Employee objects, including one with your first & last name, and using your email ID as the ID. Use the addEmployees method to put each Employee into the Map and the Set. Use an enhanced for loop. (You can use the getID method to retrieve the employee's ID to use as the key for the Map.)

Search the map. Prompt the user for whatever info you need to search the Map. Search using the get method.

I have tried a couple of ways, not sure which way is the correct; I commented out what I think is a possible solution, but I am not sure what else to do.

Am I creating the array of objects correctly? like this - ArrayList employees = new ArrayList(); or Employee employees = new Employee("John", "Bowman", "970");? Then I need to search, which is what addEmployees method is supposed to do but I keep getting errors.

public class EmployeeSearch {

    public static void main(String[] args) {

        public Set<Employee> empSet = new HashSet<Employee>();

        public Map<String, Employee> empMap = new HashMap<String, Employee>();
        // empMap.put("John", "970");

        ArrayList<String> employees = new ArrayList<String>();
        //empMap.put("John", employees);
        employees.add("John");
        employees.add("Bowman");
        employees.add("970");
        System.out.println("Employee:");

        for (String employeeName : employees){
            System.out.println(employeeName);
        }

        //Employee employees = new Employee("John", "Bowman", "970");
        //empSet.add("John", "Bowman", "970");
    }

    public void addEmployees(Employee[] employees) {
        //  empSet.add(employees[0]);
        //  empMap.put(employees[0].getID(),employees[0]);
        //  for(empSet.add(employees[e])):empMap.put(employees[e]){

    }

}

----------------------UPDATE--------------------------------------------

public class EmployeeSearch {
public static void main(String[] args) {
    Set<Employee> empSet = new HashSet<Employee>();

   public Map<String, Employee> empMap = new HashMap<String, Employee>();

   public Employee emp1 = new Employee("John", "Bowman", "970");

    ArrayList<Employee> employees = new ArrayList<Employee>();

    employees.add(emp1);
    empMap.put("970", emp1);


    for (Employee employeeName : employees) {
        System.out.println(employeeName);
    }
}




    public void addEmployees(Employee[] employees){
        empSet.add(addEmployees(employees));
        empMap.put(employees.getID(), addEmployees(employees));
        Employee searchedFor = empMap.get(id);
        if (searchedFor == null) {
            System.out.println();
        }else{
                System.out.println();
            }
        }

        }

I keep getting "cannot resolve symbol" errors and It says to "Use the addEmployees method to put each Employee into the Map and the Set." I am pretty sure I did this in main and cannot figure out how to put it in the addEmployees method.

Upvotes: 1

Views: 5967

Answers (2)

Anonymous
Anonymous

Reputation: 86296

The basic trick is — you have guessed correctly:

    Employee oneEmployee = new Employee("John", "Bowman", "970");
    empSet.add(oneEmployee);
    empMap.put(oneEmployee.getID(), oneEmployee);

This requires: Your Employee class should have the appropriate 3-argument constructor (maybe it already has, otherwise write one). And in addition to empSet you need to declare empMap.

EDIT:

I understand from your comment that you need to pass an array of employees to an addEmployees method. There are many ways to create and fill such an array. The easiest is to use an array initializer:

Employee[] allEmployees = { new Employee("John", "Bowman", "970"),
                            new Employee("John", "Rawsick", "6"),
                            new Employee("BlackHat", "Samurai", "95")
                          };

Make the array as long as you wish.

Inside the addEmployees method is where you can use the so-called enhanced for loop (another requirement in the assignment) to go through the employees and add each one to both the set and the map using the basic trick I showed at the top.

Searching for an empoyee by ID is a lookup in your map. Assume you have String id containing the user input — you’ve already been told to use the get method:

    Employee employeeSearchedFor = empMap.get(id);
    if (employeeSearchedFor == null) {
        System.out.println("No employee with ID " + id + " found";
    } else {
        // print the employee
    }

Disclaimer: I have not compiled and run the code, there might be a typo somewhere.

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

If you are trying to add an employee to an array list, all you have to do is:

//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");

//Create array list
ArrayList<Employee> employees = new ArrayList<Employee>();

//Add employee to list
employees.add(employee);

//repeat as needed for each employee

If you want a hashmap you do:

//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");

//Create array list
HashMap<String, Employee> employees = new ArrayList<String, Employee>();

//Add employee to list
employees.put("[email protected]", employee);

//repeat as needed for each employee

If it were me, I'd use a Hashmap, as you can search it really easy, using the email in this case, by doing:

    //search for employee
   if( employees.containsKey("[email protected]")){
     //retrieve employee object
     Employee myGuy = employees.get("[email protected]");

   }

Upvotes: 1

Related Questions