Interview Question for Java Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.HashMap;
import 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;
}
}

- sandesh udupi February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice..

- amit deol February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Fabrice January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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..

- sandesh udupi January 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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;

}

}

}

- Sunny January 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Overrride the HashMap class. Instead of one HashMap have two HashMaps to store the data. Override the get Method in your classs and check the type of Key passed to it by typecasting and then decide on which HashMap you want to run the GET Operation.

- Abhi January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correction you can implement the common interface by both the class

- sunny bindal February 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use Gauva BiMap.

- Neha February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction .. Guava!!!

- Neha February 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks all for the reply

- sandesh udupi February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- mahesh March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OutPut :

key  per   Value   Employee [Mname=hhhhh]
key  emp   Value   Person [id=1, name=jjjj]

- mahesh March 12, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More