Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write POJO class as a key to hashmap???

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain JMS in detail.

978


What is immutable state?

954


What does it mean that a class or member is final?

953


What is the synonym of framework?

949


What are the advantages of java?

939


What is passing parameters in java?

995


What is the difference between a method and a function in alice?

1116


can any body body expalin best definitions & best real time exaples for opps concepts.

2224


why not override thread to make a runnable? : Java thread

963


Is java code slower than native code?

1013


What is the difference between an object-oriented programming language and object-based programming language?

959


Is logger a singleton?

899


What is the difference between @before and @beforeclass annotation?

1032


What is a lambda expression ? What's its use ?

1063


What is the purpose of lambda expressions?

1009