Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
I assume you want the reference to the class as value so
Hashmap<String,Object> where object could be an interface implemented by both employee and person
the key of the employee is empname + title
the key of the person is age + person + (one letter gender>
since empname and person are the same I try to make sure they are not in the same position to help the hash code function of the String to distribute better the values
thanks for your reply....
the hashmap is something like <Employee,Person> ... where in if i give employee as the key i should get person value and person as key should get employee as value... two way hashmap concept need to applied which i am not familiar..
package com.my.Test;
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj==null)
return false;
if(this.getClass() != obj.getClass())
return false;
Person p = (Person)obj;
if(name == null)
{
if(p.getName()!= null)
return false;
}
else if(!name.equalsIgnoreCase(p.getName()))
return false;
else if(age != p.getAge())
return false;
else if(gender == null)
{
if(p.gender != null)
return false;
}
else if(!gender.equalsIgnoreCase(p.getGender()))
return false;
return true;
}
@Override
public int hashCode(){
final int primaryKey = 31;
int result = 1;
result = primaryKey * result + ((name == null) ? 0: name.hashCode());
result = primaryKey * result + age;
result = primaryKey * result + ((gender == null) ? 0 : gender.hashCode());
return result;
}
}
package com.my.Test;
public class Employee {
private String empName;
private String Title;
public Employee(String empName, String title) {
super();
this.empName = empName;
Title = title;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((Title == null) ? 0 : Title.hashCode());
result = prime * result + ((empName == null) ? 0 : empName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (Title == null) {
if (other.Title != null)
return false;
} else if (!Title.equals(other.Title))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
return true;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
}
package com.my.Test;
import java.util.HashMap;
import java.util.Map;
public class Test extends HashMap<Object, Object> {
public static void main(String[] args) {
Employee e = new Employee("abc", "man");
Person p = new Person("abc", "m", 21);
Employee e1 = new Employee("def", "man");
Person p1 = new Person("def", "f", 21);
Test ep = new Test();
ep.put(e, p);
ep.put(p1, e1);
ep.get(e1);
}
@Override
public Object get(Object key)
{
Person p = null;
Employee e = null;
if(key.getClass().equals(Employee.class))
{
if(super.get(key) != null)
{
p = (Person)super.get(key);
}
else if (containsValue(key))
{
for(Map.Entry en : this.entrySet())
{
if(en.getValue().equals(key))
p = (Person)en.getKey();
break;
}
}
System.out.println(p.getName() + p.getAge() + p.getGender());
return p;
}
else
{
if(super.get(key) != null)
{
e = (Employee)super.get(key);
}
else if (containsValue(key))
{
for(Map.Entry en : this.entrySet())
{
if(en.getValue().equals(key))
e = (Employee)en.getKey();
break;
}
}
System.out.println(e.getEmpName() + e.getTitle());
return e;
}
}
}
you can create a implement a common interface by both the class and then use that common interface to create the map.
public class Test {
public static void main(String[] args) {
Map<Common, Common> map = new HashMap<Common, Common>();
A a = new A();
a.name = "sunny";
B b = new B();
b.age = 28;
map.put(a, b);
System.out.println(map.get(a));
A a1 = new A();
a1.name = "bindal";
B b1 = new B();
b1.age = 18;
map.put(b1, a1);
System.out.println(map.get(b1));
}
}
interface Common {
}
class A implements Common {
String name;
@Override
public String toString() {
return this.name;
}
}
class B implements Common {
int age;
@Override
public String toString() {
return this.age + "";
}
}
class Person
{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
class Employee
{
private String Mname;
public String getMname() {
return Mname;
}
public void setMname(String mname) {
Mname = mname;
}
@Override
public String toString() {
return "Employee [Mname=" + Mname + "]";
}
}
public class A {
public static void main(String[] args) {
Employee emp=new Employee();
emp.setMname("hhhhh");
Person per=new Person();
per.setId(1);
per.setName("jjjj");
HashMap<String,Object> h=new HashMap<String,Object>();
h.put("emp", per);
h.put("per", emp);
for(String s:h.keySet())
{
System.out.println("key "+ s+ " Value "+h.get(s));
}
}
}
import java.util.HashMap;
- sandesh udupi February 12, 2014import java.util.Map;
public class Main extends HashMap<Object, Object> {
public static void main(String args[]) {
Main empObj = new Main();
Person personRef1 = new Person();
personRef1.setAge(30);
Employee empRef1 = new Employee();
empRef1.setEmpName("Ram");
Person personRef2 = new Person();
personRef2.setAge(28);
Employee empRef2 = new Employee();
empRef2.setEmpName("Sandesh");
empObj.put(empRef1, personRef1);
empObj.put(empRef2, personRef2);
Person p3 = (Person) empObj.getValue(empRef2);
Employee e3 = (Employee) empObj.getValue(personRef1);
System.out.println("Person value:" + p3.getAge());
System.out.println("Employee value:" + e3.getEmpName());
}
public Object getValue(Object key) {
if (this.containsKey(key)) {
return this.get(key);
} else {
for (Map.Entry<Object, Object> entry : this.entrySet()) {
if (entry.getValue() == key) {
return entry.getKey();
}
}
}
return null;
}
}
class Person{
private int age;
private String gender;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
class Employee{
private String empName;
private String title;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}