sandesh udupi
BAN USER
Questions (1)
Comments (3)
Reputation 20
- 0of 0 votes
Answersi have 2 class class employee which has empname and Title(manager,team lead) and class person which has name age and gender.. now i need to create a hasmap which has key and value pair as employee and person and if i give employee as key it should give me person values and if i give person as key it should give me employee value
- sandesh udupi in India| Report Duplicate | Flag | PURGE
Java Developer Java
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
thanks all for the reply
- sandesh udupi February 12, 2014Comment hidden because of low score. Click to expand.
0
of 0 vote
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..
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
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;
}
}