Write POJO class as a key to hashmap???
Answers were Sorted based on User's Feedback
Answer / shiv prakash
Class must be final
Class must implement Comparable interface(compareTo() method)
Must override hashCode() and equals() methods
Class must implement Serializable interface
For example- all wrapper classes-Integer, Number, Character, and String
Own class
package com;
import java.io.Serializable;
public final class FinalPerson implements Serializable, Comparable<FinalPerson>{
private static final long serialVersionUID = 1L;
private final Integer personId;
private final String name;
private final String city;
private final String gender;
public FinalPerson(final Integer personId, final String name, final String city, final String gender) {
this.personId = personId;
this.name = name;
this.city = city;
this.gender = gender;
}
public Integer getPersonId() {
return personId;
}
public String getCity() {
return city;
}
public String getGender() {
return gender;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person- name:"+this.getName()+", City:"+this.getCity()+",gender:"+this.getGender();
}
public int compareTo(FinalPerson p) {
return this.getName().compareTo(p.getName());
}
@Override
public boolean equals(Object obj) {
FinalPerson p = (FinalPerson)obj;
return this.getPersonId().equals(p.getPersonId());
}
@Override
public int hashCode() {
int hash = 7;
hash = 31* hash + this.getPersonId();
return hash;
}
}
Is This Answer Correct ? | 6 Yes | 0 No |
Answer / sudha
Map p=new HashMap();
PojoClass pc=new PojoClass();
p.put("key",pc);
Is This Answer Correct ? | 2 Yes | 12 No |
How to do a true java ping from windows?
What is interface and its use?
What is the difference between Trusted and Untrusted Applet ?
What is an object’s lock and which object’s have locks?
How to sort double array in java?
Why we need to serialize the object
11 Answers CTS, Geometric Software,
What do you mean by stream pipelining in java 8?
What is a jit compiler?
How the elements are organized in GridLayout?
Why cant we define System.out.println() inside a class directly?
What is an object class?
In Java list the methods that can be overridden?