SearchByEmail print - java

I have an EmployeeStore class that stores details for employee's in a company such as Name, id and e-mail. I need a method to search the hashmap by the e-mail addresses of its employees and then print out that individual employee with the matching address.
I got the code to search the map in another answer but can't manage to print the employee.
Here is my code:
public class EmployeeStore {
HashMap<String, Employee> map;
public EmployeeStore() {
map = new HashMap<String, Employee>();
}
//....
public void add(Employee employee) {
map.put(employee.getEmployeeName(), employee);
}
public Employee searchByName(String name) {
Employee employee = map.get(name);
System.out.println(employee);
return employee;
}
public Employee searchByEmail(String email) {
for (Employee employee : map.values()) {
if (email.equals(employee.getEmployeeEmail())) {
return employee;
}
System.out.println(employee);
}
Employee employee = map.get(email);
System.out.println(employee);
return employee;
}
}
To do this i changed my code to this:
public Employee searchByEmail(String email)
{
for (Employee employee : map.values())
{
if (email.equals(employee.getEmployeeEmail()))
{
System.out.println(employee);
return employee;
}
}
return null;
}
Main:
public class MainApp {
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
new MainApp().start();
}
public void start() {
EmployeeStore Store = new EmployeeStore();
Store.add(new Employee("James O' Carroll", 18, "hotmail.com"));
Store.add(new Employee("Andy Carroll", 1171, "yahoo.com"));
Store.add(new Employee("Luis Suarez", 7, "gmail.com"));
Store.searchByName("James O' Carroll");
//Store.print();
}
}

Your searchByxxx methods return an Employee so you should read that value then it is easy to print it:
Employee james = Store.searchByName("James O' Carroll");
Employee andy = Store.searchByEmail("yahoo.com");
System.out.println(james);
System.out.println(andy);
and you need to clean the searchByXXX methods a little and remove the unnecessary print statements.

Related

Trying to learn about encapsulation example question included

So I have done everything the task requires in the code below.
The problem I am have is about the
Defense copy's of a Object
Change the values in the Employee object.
For 2
I change the values but I call new which is creating a new instance of the object.
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
This way the problem works as the second time I call mainDepartment.display();the values are the same.
But this does not feel like proper encapsulation as i am creating a new object.
I was thinking of
employee1.setName("Conor Bryan);
Is how you test for encapsulation, now when I call mainDepartment.display(); the name value does change so the question is wrong.
!-----Question-----!
Display the Employee details
Display the department details
Change the values in the Employee object.
Display the Department details again (The information should not change)
Again display the Employee details (The information should be changed here).
!----Test----!
package question1;
public class Test {
public static void main(String[] args) {
//Creating a instance of both Employee and Department
Employee employee1 = new Employee("2726354E", "Bob Ings", 30000 );
//Updated Code for Department to take a copy of Employee
Employee copy = new Employee("2726354E", "Bob Ings", 30000 );
Department mainDepartment = new Department("Main Floor", copy);
//Displaying both instances of Employee and Department
employee1.display();
mainDepartment.display();
System.out.println("");
//Changing values in constructor for the instance of Employee we made earlier on
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
mainDepartment.display();
System.out.println("");
System.out.println("");
employee1.display();
}
}
!----Department----!
package question1;
public class Department {
private String deptName;
private Employee employee;
private int officeNumber;
//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = officeNumber;
}
//Constructor with the officeNumber set to 0
public Department(String deptName, Employee employee) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = 0;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Department");
Employee.seperationLine();
System.out.println("Department Name: " + getDeptName());
Employee.seperationLine();
System.out.println("Employee: " + employee.toString());
Employee.seperationLine();
System.out.println("Office Number: " + getOfficeNumber());
}
//Getters and Setters
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getOfficeNumber() {
return officeNumber;
}
public void setOfficeNumber(int officeNumber) {
this.officeNumber = officeNumber;
}
}
!----Employee----!
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
public Employee() {}
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = salary;
}
//Defensive copy constructor
public Employee(Employee copy) {
this.ppsNum = copy.ppsNum;
this.name = copy.name;
this.salary = copy.salary;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Employee Information");
seperationLine();
System.out.println("Name: " + getName());
seperationLine();
System.out.println("PPS number: " + getPpsNum());
seperationLine();
System.out.println("Salary: " + getSalary() + "0");
seperationLine();
System.out.println("\n");
}
public String toString() {
return "[ppsNum=" + ppsNum + ", name=" + name + ", salary=" + salary + "]";
}
//Getters and Setters
public String getPpsNum() {
return ppsNum;
}
public void setPpsNum(String ppsNum) {
this.ppsNum = ppsNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void seperationLine() {
System.out.println("------------------------");
}
}
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
is not changing the values in an Employee object, it is creating a new Employee object.
I was thinking of
employee1.setName("Conor Bryan);
Yes, this is the correct way to change a value in an object. Do that instead of creating a new object, then display the result and you should see that the value of employee1 changed.
In order to fulfill the second requirement (that Department doesn't change) you need to make a copy of the Employee when you pass it to Department.
//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {
this.deptName = deptName;
this.employee = employee; // make a copy here
this.officeNumber = officeNumber;
}
The easiest way to do this is probably to implement a copy constructor in your Employee class, then use that in Department. You'd need to make a copy anywhere you set the employee attribute in Department, so in both of your constructors and in the setEmployee method.

Collections.sort does not sort my list of objects

I'm trying to sort a list of objects by its id. Here is my code:
List<Employee> employee = getEmployeeList();
Collections.sort(employee, new Comparator<Employee>(){
#Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
}
});
But nothing happens after the sort, it still shows the original employee list. Am I doing something wrong? I've searched everywhere but they all just do this and it works for them. Here is a hard coded set of Employee Ids:
public class Employee{
private String employeeId = "";
private String employeeName = "";
private String contactNbr = "";
//getters and setters
}
List<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setEmployeeId("A1B1");
empList.add(emp1);
Employee emp2 = new Employee();
emp2.setEmployeeId("A2B1");
empList.add(emp2);
Employee emp3 = new Employee();
emp3.setEmployeeId("A3B1");
empList.add(emp3);
Collections.sort(empList, new Comparator<Employee>(){
#Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
}
});
Check the results of the employeeId compareTo method.
If it always returns 0, the sort will think all the elements are equal. Collections does a stable sort, so if all the elements are equal it will leave the order unchanged.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class MainTest {
public static void main(String[] arg)
{
List<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setEmployeeId("A2B1");
empList.add(emp1);
Employee emp2 = new Employee();
emp2.setEmployeeId("A1B1");
empList.add(emp2);
Employee emp3 = new Employee();
emp3.setEmployeeId("A3B1");
empList.add(emp3);
Collections.sort(empList, new Comparator<Employee>(){
public int compare(Employee employee1, Employee employee2) {
return employee1.getEmployeeId().compareTo(employee2.getEmployeeId());
}
});
Iterator i = empList.iterator();
while(i.hasNext()){
System.out.println(((Employee)i.next()).getEmployeeId());
}
}
}
class Employee{
private String employeeId = "";
private String employeeName = "";
private String contactNbr = "";
public String getContactNbr() {
return contactNbr;
}
public void setContactNbr(String contactNbr) {
this.contactNbr = contactNbr;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
//getters and setters
}
This is printing correctly output:
A1B1
A2B1
A3B1
You are adding the list in the sorted order that's why you are under the impression that your list is not getting the updated. As far as your source is concerned there is no issues you are doing it right. The issue is your input. You are giving the following input in the sorted order as follows.
emp1.setEmployeeId("A1B1");
empList.add(emp1);
emp1.setEmployeeId("A2B1");
empList.add(emp2);
emp1.setEmployeeId("A3B1");
empList.add(emp3);
Your output will be as below
Before sorting: [A1B1, A2B1, A3B1]
After sorting: [A1B1, A2B1, A3B1]
Try modifying the source as below and check
emp1.setEmployeeId("A3B1");
empList.add(emp3);
emp1.setEmployeeId("A2B1");
empList.add(emp2);
emp1.setEmployeeId("A1B1");
empList.add(emp1);
You will get the result as below
Before sorting: [A3B1, A2B1, A1B1]
After sorting: [A1B1, A2B1, A3B1]

Delete and SearchByEmail methods not working

Hey my Delete and SearchByEmail methods are not working. Delete basically asks all the right questions but does not delete the Employee the user chooses. SearchByEmail returns all of the employees rather than the one the user has asked for.
Here is my code:
MainApp
//Imports.
import java.util.Scanner;
//********************************************************************
public class MainApp
{
//The Scanner is declared here for use throughout the whole MainApp.
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
new MainApp().start();
}
public void start()
{
//Create a Store named Store and add Employee's to the Store.
EmployeeStore Store = new EmployeeStore();
Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com"));
Store.add(new Employee ("Luis Suarez", 7,"gmail.com"));
//********************************************************************
..............................
case 3:
System.out.println("Delete");
Employee employeeDelete = MenuMethods.userInputByName();
Store.searchByName(employeeDelete.getEmployeeName());
Store.remove(employeeDelete);
break;
case 4:
System.out.println("Delete All");
Store.clear();
break;
case 5:
System.out.println("Edit");
Employee employeeEdit = MenuMethods.userInputByName();
Store.searchByName(employeeEdit.getEmployeeName());
if (employeeEdit != null)
{
employeeEdit.setEmployeeName("Joe");
employeeEdit.setEmployeeId(1);
employeeEdit.setEmployeeEmail("webmail.com");
Store.edit(employeeEdit);
}
break;
case 6:
int searchChoice;
searchChoice =MenuMethods.getMenuChoice("1.Search by Name.\n2.Search by Email.", 2, "Please enter your choice:", "Error [1,2] Only");
String temp = keyboard.nextLine();
System.out.println("Search");
switch (searchChoice) {
case 1:
System.out.println("Search by Name.");
Employee employeeSearchName = MenuMethods.userInputByName();
Store.searchByName(employeeSearchName.getEmployeeName());
break;
case 2:
System.out.println("Search by Email.");
Employee employeeSearchEmail = MenuMethods.userInputByEmail();
Store.searchByEmail(employeeSearchEmail.getEmployeeEmail());
break;
//Test Code.
/*System.out.println("Search");
Employee employee1 = MenuMethods.userInputByName();
Employee foundEmployee = Store.searchByName(employee1.getEmployeeName());
if (foundEmployee != null) {
System.out.println("Found employee!");
} else {
System.out.println("Not Found!");
}*/
}
break;
case 7:
System.out.println("Print");
Store.print();
break;
case 8:
System.out.println("Exit");
break;
}
} while (choice != 8);
}
}
MenuMethods
//Imports
import java.util.Scanner;
//********************************************************************
public class MenuMethods
{
private static Scanner keyboard = new Scanner(System.in);
..............................
public static Employee userInput()
{
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee ID:");
int employeeId = keyboard.nextInt();
temp = keyboard.nextLine();
System.out.println("Please enter the Employee E-mail address:");
String employeeEmail = keyboard.nextLine();
return e = new Employee(employeeName , employeeId, employeeEmail);
}
//********************************************************************
public static Employee userInputByName()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
return e = new Employee(employeeName);
}
//********************************************************************
public static Employee userInputByEmail()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Email:");
String employeeEmail = keyboard.nextLine();
//This can use the employeeName's constructor because java accepts the parameters instead
//of the name's.
return e = new Employee(employeeEmail);
}
//********************************************************************
}
EmployeeStore
//Imports.
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//********************************************************************
public class EmployeeStore implements Serializable
{
HashMap<String, Employee> map;
private static Scanner keyboard = new Scanner(System.in);
//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public Employee remove(Employee key)
{
//Remove the Employee by name.
if(map.containsKey(key))
return map.remove(key); //if its there remove and return
else
return null; //if its not there return 0x00000000 address
}
//********************************************************************
//Clear the Hashmap : Employee.
public void clear()
{
map.clear();
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for (Employee employee : map.values())
{
//System.out.println(employee); to print the toString of Employee class
//or:
System.out.println("Employee Name:\t" + employee.getEmployeeName());
System.out.println("Employee Id:\t" + employee.getEmployeeId());
System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
}
}
public Employee get(String name){
return map.get(name);
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
}
}*/
//********************************************************************
public Employee searchByName(String employeeName)
{
Employee employee = map.get(employeeName);
System.out.println(employee);
return employee;
}
//********************************************************************
Employee findByEmail(String email) {
for (Employee employee : map.values())
if (employee.getEmployeeEmail().equals(email))
return employee;
// Not found.
return null;
}
public boolean searchByEmail(String employeeEmail) {
boolean employee = findByEmail(employeeEmail) != null;
System.out.println(employee);
return employee;
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
System.out.println(e);
map.equals(employee.getEmployeeEmail());
}
}*/
//********************************************************************
public void edit(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
public EmployeeStore copy()
{
//instanciate the destination copy and give same name
EmployeeStore Copy = new EmployeeStore();
//by specifying the type of the entry in the for loop i.e. <>
// we don't have to typecast the getKey() and getValue() methods
//as shown in the commented out line inside the for loop
for(Map.Entry<String, Employee> entry : map.entrySet())
{
//getting each key-value and putting into the copy
//theCopy.add((MovieKey)entry.getKey(), (Movie)entry.getValue());
Copy.add(entry.getValue());
}
//return address of the new MovieStore object
return Copy;
}
//********************************************************************
//********************************************************************
//********************************************************************
}
Employee
//Imports:
//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
private String employeeName;
private int employeeId;
private String employeeEmail;
//********************************************************************
//Constructors.
public Employee(String employeeName, int employeeId, String employeeEmail)
{
this.employeeName = employeeName;
this.employeeId = employeeId;
this.employeeEmail = employeeEmail;
}
//Overloading the constructor for the use with userInputByName method.
public Employee(String employeeName)
{
this.employeeName = employeeName;
}
//********************************************************************
//Getters.
public String getEmployeeEmail()
{
return employeeEmail;
}
public String getEmployeeName()
{
return employeeName;
}
public int getEmployeeId()
{
return employeeId;
}
//********************************************************************
//Setters.
public void setEmployeeEmail(String employeeEmail)
{
this.employeeEmail = employeeEmail;
}
public void setEmployeeName(String employeeName)
{
this.employeeName = employeeName;
}
public void setEmployeeId(int employeeId)
{
this.employeeId = employeeId;
}
//********************************************************************
//toString method.
public String toString()
{
return "\t\t\tEmployee\n" +
"********************************************************************\n"+
"Employee Name: "+ employeeName +"\n"+
"Employee Id: " + employeeId +"\n"+
"Employee Email: " + employeeEmail;
}
//********************************************************************
}
If you have provided your Employee class it should be more clear, anyways.
The problem has to be the following:
return e = new Employee(employeeEmail);
So it is about your Employee Constructor which is defined for employeeName but you initialize employee object with employeeEmail. Therefore, it assigns email as a name..
Just return the search phrase as a String don't create new Employee objects for searches or other operations.
Without destructing your structure, I would suggest you to add another constructor to Employee class
public Employee(String employeeName,String employeeEmail)
{
this.employeeName = employeeName;
this.employeeEmail = employeeEmail;
}
and
public static Employee userInputByEmail()
{
...
...
return e = new Employee(null,employeeEmail);
}
You appear to be using
Employee(String employeeName)
in 2 different ways:
return e = new Employee(employeeName);
and
return e = new Employee(employeeEmail);
The field should be one or the other. Perhaps just create Employee() and use setEmployeeEmail.

Edit method for Employee Store.(Using Hashmap)

Hey can anyone help me understand how to do an edit method for my company application. I had previously asked for help with a search method. And i think the edit method might involve the search method.
Here is my code:
EmployeeStore.
//Imports.
import java.util.HashMap;
//********************************************************************
public class EmployeeStore
{
HashMap<String, Employee> map;
//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public void remove(String key)
{
//Remove the Employee by name.
map.remove(key);
}
//********************************************************************
//Clear the Hashmap : Employee.
public void clear()
{
map.clear();
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for (Employee employee : map.values())
{
//System.out.println(employee); to print the toString of Employee class
//or:
System.out.println("Employee Name:\t" + employee.getEmployeeName());
System.out.println("Employee Id:\t" + employee.getEmployeeId());
System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
}
}
public Employee get(String name){
return map.get(name);
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
}
}*/
//********************************************************************
public Employee searchByName(String name)
{
Employee employee = map.get(name);
System.out.println(employee);
return employee;
}
//********************************************************************
public Employee searchByEmail(String email)
{
for (Employee employee : map.values())
{
if (email.equals(employee.getEmployeeEmail()))
{
System.out.println(employee);
return employee;
}
}
return null;
}
//********************************************************************
//********************************************************************
//********************************************************************
}
Employee class.
//Imports:
//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
private String employeeName;
private int employeeId;
private String employeeEmail;
//********************************************************************
//Constructor.
public Employee(String employeeName, int employeeId, String employeeEmail)
{
this.employeeName = employeeName;
this.employeeId = employeeId;
this.employeeEmail = employeeEmail;
}
//********************************************************************
//Getters.
public String getEmployeeEmail() {
return employeeEmail;
}
public void setEmployeeEmail(String employeeEmail) {
this.employeeEmail = employeeEmail;
}
public String getEmployeeName() {
return employeeName;
}
public int getEmployeeId() {
return employeeId;
}
//********************************************************************
//toString method.
public String toString() {
return "\t\t\tEmployee\n" +
"********************************************************************\n"+
"Employee Name: "+ employeeName +"\n"+
"Employee Id: " + employeeId +"\n"+
"Employee Email: " + employeeEmail;
}
//********************************************************************
}
You can use the put method of java's HashMap for this as well. From the API for HashMap's put method:
If the map previously contained a mapping for this key, the old value is replaced.
So, something like:
public void edit(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
And then in the other code:
Employee employee = getEmployeeByName("Someniceemployeename");
if (employee != null)
{
employee.setEmployeeEmail("awesomeness#stackoverflow.com");
edit(employee);
}
As for editing the ID of an employee, you need to take some additional precautions. First, you want to make sure that the map contains the ID you want to edit (as usual). Second, when "editing" the ID you need to first remove the old employee instance from the map (with the old ID) and then add the new employee with put.

Finding Minimum For every group in ArrayList in Java

I have a class as below.
public class Employee{
int EmployeeDepartmentId;
int EmplyoyeeId;
int Salary;
}
I an creating an ArrayList as Below.
ArrayList<Employee> arrEmployee = new ArrayList<Employee>;
Now, I want to find out List of employees from every department with minimum salary.
How can this be achieved. I tried using Collections.Min(), but that will give me the minimum salary from whole list. I want it to be grouped by department.
Any suggestions?
Do two runs. The first to group the employees by department, perhaps using a map:
Map<Integer,List<Employee>>
Then loop over this map grabbing the minimum of each list.
Using the idea by Jim, something like this should work:
Map<Integer, List<Employee>> hmap = new HashMap<Integer, List<Employee>>();
int i = 0;
int size = arrEmployee.size();
for(i = 0; i < size; i++)
{
Employee emp = arrEmployee.get(i);
List<Employee> list = hmap.get(emp.EmployeeDepartmentId);
if (list == null)
{
list = new ArrayList<Employee>();
}
list.add(emp);
hmap.put(emp.EmployeeDepartmentId, list);
}
You should make your own comparator :
Collections.min(list,new Comparator<Employee>() {
public int compare(Employee e1, Employee e2) {
return e2.getSalary().compareTo(e1.getSalary());
}
});
I Hope this help.
This will only get the minimum salary.
every logic is in Comparator they will be grouped by department and sorted by salary:
public class Employee {
Integer EmployeeDepartmentId;
Integer EmplyoyeeId;
Integer Salary;
public Employee(Integer employeeDepartmentId, Integer emplyoyeeId, Integer salary) {
EmployeeDepartmentId = employeeDepartmentId;
EmplyoyeeId = emplyoyeeId;
Salary = salary;
}
public Integer getEmployeeDepartmentId() {
return EmployeeDepartmentId;
}
public void setEmployeeDepartmentId(Integer employeeDepartmentId) {
EmployeeDepartmentId = employeeDepartmentId;
}
public Integer getEmplyoyeeId() {
return EmplyoyeeId;
}
public void setEmplyoyeeId(Integer emplyoyeeId) {
EmplyoyeeId = emplyoyeeId;
}
public Integer getSalary() {
return Salary;
}
public void setSalary(Integer salary) {
Salary = salary;
}
public static void main(String[] args) {
List<Employee> employees=new ArrayList<Employee>();
employees.add(new Employee(3, 3, 50));
employees.add(new Employee(2, 4, 2500));
employees.add(new Employee(1, 1, 100));
employees.add(new Employee(2, 5, 1100));
employees.add(new Employee(1, 2, 20));
for(Employee e:employees)
System.out.println(e.getEmployeeDepartmentId()+" "+e.getEmplyoyeeId()+" "+e.getSalary());
Collections.sort(employees,new Comparator<Employee>() {
#Override
public int compare(Employee e, Employee e1) {
if(e.getEmployeeDepartmentId()==e1.getEmployeeDepartmentId()){
return e.getSalary().compareTo(e1.getSalary());
}else
return e.getEmployeeDepartmentId().compareTo(e1.getEmployeeDepartmentId());
}
});
for(Employee e:employees)
System.out.println(e.getEmployeeDepartmentId()+" "+e.getEmplyoyeeId()+" "+e.getSalary());
}
}
You'll have to do it youself:
Map<Integer, Employee> minSalary = new HashMap<Integer, Employee>();
for(Employee emp : employeeList) {
Integer depId = emp.getDepartmentId();
if(minSalary.containsKey(depId )) {
if(emp.getSalary() < minSalary.get(depId).getSalary()) {
minSalary.put(depId, emp);
}
}
else {
minSalary.put(depId, emp);
}
}
Note that only one Employee is stored with minimal salary, but it is sufficient when you only want to know the salary. If you really need all employees with the minimal salary, you need a Map<Integer, List<Employee>>.
In that case i'd recommend to store the Employees redundantly in a List<Employee> and a `Map<Integer, List<Employee>> (per departmentID, as suggested by other answers.

Categories

Resources