Advertisement
thewitchking

Untitled

May 30th, 2025
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Employee {
  4.     private int empNo;
  5.     private String name;
  6.     private Set<String> skills;
  7.  
  8.     public Employee(int empNo, String name, Set<String> skills) {
  9.         this.empNo = empNo;
  10.         this.name = name;
  11.         this.skills = new HashSet<>(skills); // defensive copy
  12.     }
  13.  
  14.     public int getEmpNo() {
  15.         return empNo;
  16.     }
  17.  
  18.     public String getName() {
  19.         return name;
  20.     }
  21.  
  22.     public Set<String> getSkills() {
  23.         return new HashSet<>(skills); // return copy to maintain immutability
  24.     }
  25.  
  26.     @Override
  27.     public boolean equals(Object o) {
  28.         if (this == o) return true;
  29.         if (!(o instanceof Employee)) return false;
  30.         Employee employee = (Employee) o;
  31.         return empNo == employee.empNo &&
  32.                Objects.equals(name, employee.name) &&
  33.                Objects.equals(skills, employee.skills);
  34.     }
  35.  
  36.     @Override
  37.     public int hashCode() {
  38.         return Objects.hash(empNo, name, skills);
  39.     }
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return "Employee{empNo=" + empNo + ", name='" + name + "', skills=" + skills + "}";
  44.     }
  45. }
  46.  
  47. public class EmployeeMapExample {
  48.     public static void main(String[] args) {
  49.         Employee emp1 = new Employee(1001, "Alice", Set.of("Java", "Spring", "SQL"));
  50.         Employee emp2 = new Employee(1002, "Bob", Set.of("Python", "Django", "ML"));
  51.         Employee emp3 = new Employee(1003, "Charlie", Set.of("JavaScript", "React", "Node.js"));
  52.  
  53.         Map<Employee, String> employeeRoles = new HashMap<>();
  54.         employeeRoles.put(emp1, "Backend Developer");
  55.         employeeRoles.put(emp2, "Data Scientist");
  56.         employeeRoles.put(emp3, "Frontend Developer");
  57.  
  58.         // Display all employee-role mappings
  59.         for (Map.Entry<Employee, String> entry : employeeRoles.entrySet()) {
  60.             System.out.println(entry.getKey() + " -> " + entry.getValue());
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement