Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing the employee data in a map in java

Tags:

java

In an interview it was asked to design a POJO named Employee which consists of three fields:

  1. name
  2. job
  3. salary

And then store it in a set and then remove the objects for which I have design the program as shown below

but later on they have asked me to store it in has map also now my problem is that what should i use as key in map since in this case the name job salary cab be same also so what shall i use as a key in hash map

class Emp
  implements Comparable
{
  String name;
  String job;
  int salary;

  public Emp(String paramString1, String paramString2, int paramInt)
  {
    this.name = paramString1;
    this.job = paramString2;
    this.salary = paramInt;
  }

  public void display() {
    System.out.println(this.name + "\t" + this.job + "\t" + this.salary);
  }

  public boolean equals(Object paramObject) {
    Emp localEmp = (Emp)paramObject;
    return (this.name.equals(localEmp.name)) && (this.job.equals(localEmp.job)) && (this.salary == localEmp.salary);
  }

  public int hashCode() {
    return this.name.hashCode() + this.job.hashCode() + this.salary;
  }

  public int compareTo(Object paramObject) {
    Emp localEmp = (Emp)paramObject;
    return this.name.compareTo(localEmp.name);
  }
}

and the main class is

class EmpHsDemo
{
  public static void main(String[] paramArrayOfString)
  {
    HashSet localHashSet = new HashSet();
    localHashSet.add(new Emp("Ram", "Trainer", 34000));
    localHashSet.add(new Emp("Ravi", "Administrator", 44000));
    localHashSet.add(new Emp("Sachin", "Programmer", 24000));
    localHashSet.add(new Emp("Priyanka", "Manager", 54000));
    localHashSet.add(new Emp("Anupam", "Programmer", 34000));
    localHashSet.add(new Emp("Sachin", "Team Leader", 54000));
    System.out.println("There are " + localHashSet.size() + " elements in the set.");
    System.out.println("Content of set are : ");
    Iterator localIterator = localHashSet.iterator();
    while (localIterator.hasNext())
    {
      localEmp1 = (Emp)localIterator.next();
      System.out.print(localEmp1.hashCode() + "\t");
      localEmp1.display();
    }
    Emp localEmp1 = new Emp("Ravi", "Administrator", 44000);
    System.out.println("Removing following Emp from the set...");
    System.out.print(localEmp1.hashCode() + "\t");
    localEmp1.display();
    localHashSet.remove(localEmp1);
    System.out.println("No. of elements after removal " + localHashSet.size());
    Emp localEmp2 = new Emp("Anupam", "Programmer", 34000);
    System.out.println("Searching following Emp in the set...");
    System.out.print(localEmp2.hashCode() + "\t");
    localEmp2.display();
    System.out.println("Results of searching is : " + localHashSet.contains(localEmp2));
  }
}
like image 848
tuntun sswe Avatar asked Jan 16 '15 06:01

tuntun sswe


2 Answers

Your current Emp class could have problems even when used in a HashSet, since two employees with the same name can work in the same company for the same salary, and the HashSet won't be able to distinguish between them.

You should add some unique identifier to the Emp class. That unique member can serve as the key in a Map. It can also be used in the calculation of hashCode and equals (instead of the other properties), since it uniquely identifies the employee.

like image 71
Eran Avatar answered Nov 01 '22 15:11

Eran


Actually keys in HashMap is a similar thing to values in HashSet, there is no big difference between them. The only question what is unique constraints for your object, I would ask interviewer about that and suggest to use either name or add some additional unique identifier (e.g. id). If it is name + salary + job then you should use them to create equals and hashCode methods. Once these methods correctly defined - you can use Employee class itself as a HashMap key with no doubts.

Lets's say we decided that name + salary + job is our unique identificator, then we can define hashCode/equals as follows:

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Employee employee = (Employee) o;

    if (job != null ? !job.equals(employee.job) : employee.job != null) return false;
    if (name != null ? !name.equals(employee.name) : employee.name != null) return false;
    if (salary != null ? !salary.equals(employee.salary) : employee.salary != null) return false;

    return true;
}

public int hashCode() {
    int result = name != null ? name.hashCode() : 0;
    result = 31 * result + (job != null ? job.hashCode() : 0);
    result = 31 * result + (salary != null ? salary.hashCode() : 0);
    return result;
}

Now we can use Employee as a key in a Hash based structures. We can create HashSet and HashMap:

    List<Employee> employeeList = Arrays.asList(
            new Employee("name", "job", 100),
            new Employee("name", "job2", 300),
            new Employee("name2", "job", 200)
    );
    Set<Employee> hashSet = new HashSet<Employee>(employeeList);
    Map<Employee, Employee> hashMap = new HashMap<Employee, Employee>();
    for (Employee employee : employeeList) {
        hashMap.put(employee, employee);
    }

Going further to the source code of the HashSet - we see that it actually use a HashMap inside and add our values as a HashMap's keys with a dummy internal object as each Map's entry value, check the constructor and add method:

public HashSet() {
map = new HashMap<E,Object>();
}

public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
like image 44
udalmik Avatar answered Nov 01 '22 17:11

udalmik