Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<Employee, String> map2 = new HashMap<Employee, String>();
        Employee e2 = new Employee("J", 26);
        map2.put(e2, "MGMT");
        System.out.println(map2);
        e2.setAge(27);
        System.out.println(map2);
        System.out.println(map2.containsKey(e2));//false

        IdentityHashMap<Employee, String> map1 = new IdentityHashMap<Employee, String>(map2);

        System.out.println(map1);
    }
}

class Employee {
    private String name;
    private int age;

    Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Employee))
            return false;
        Employee e = (Employee) o;
        return e.name.equals(name) && e.age == age;
    }

    @Override
    public int hashCode() {
        int hashCode = 19;
        hashCode = hashCode * 31 + name.hashCode();
        hashCode = hashCode * 31 + age;
        return hashCode;
    }

    void setAge(int age) {
        this.age = age;
    }

    void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name + " " + age;
    }
}