Saving employee data on a map in java

In an interview, it was proposed to create a POJO with a name Employeethat consists of three fields:

  • name
  • Job
  • salaries

And then save it in the set, and then delete the objects for which I have the program design as shown below

but later they asked me to save it on the map. now my problem is what should I use as a key on the card, since in this case the name salary caching will also be the same that I would use as a key in the 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

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));
  }
}
+4
source share
3 answers

Emp HashSet, , HashSet .

Emp. Map. hashCode equals ( ), .

+3

HashMap HashSet, . , , , (, id). + + , equals hashCode. Employee HashMap.

, , + + , hashCode/equals :

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;
}

Employee , Hash. HashSet 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);
    }

HashSet, , HashMap HashMap , :

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

public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
+2

You can create a map as follows

 Map<String, Emp> m = new Hashtable<>();

and you can assign each employee an employee identifier and save the corresponding employee object on the map.

0
source

All Articles