Link to home
Start Free TrialLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

hashmap

I having  a Class:

class studentprofile.
{
   String name;
   String comments;;
   String others;;
}
==
Can any one help me in writing a hashmap for the above class object.
people will be entering the student name and comments.
If the person is already entered (name) than the comments should be appended to the existing comments.

you can assume that the data is ready and give me the hashmap implementation?
I am looking at how we can add data.

Avatar of for_yan
for_yan
Flag of United States of America image


-------------------------------------------------
HashMap m = new HashMap();
String name = "Peter"; //<--- read from input
String newComment = "addotional cooment";//<--- read from input

studentprofile sp = (studentprofile) m.get(name);

String oldComment = sp.getComment();

sp.setComment(oldComment + newComment);

m.put(name, sp);

------------------------------------------



sorry, HashMap shoul not be new - we assume
that you alread had HashMap with data

-------------------------------------------------
HashMap m // <---is exiting HashMap with data
String name = "Peter"; //<--- read from input
String newComment = "addotional cooment";//<--- read from input

studentprofile sp = (studentprofile) m.get(name);

String oldComment = sp.getComment();

sp.setComment(oldComment + newComment);

m.put(name, sp);

------------------------------------------


Avatar of sigma19

ASKER

Thanks Yan.
so you mean studentprofile is a class with name and new comment in it ?

2) so, if I do=

studentprofile sp = (studentprofile) m.get(name);
=>m.get returns a object if present else it returns null right?

3) Should we not mention that in the hash map declaration that
key is name and it will be having this set of objects?

I just started learning about hashmap....
Yes, you are right - in real life
you'll do it like that:

if(m.get(name) != null){
studentprofile sp = (studentprofile) m.get(name);
String oldComment = sp.getComment();

sp.setComment(oldComment + newComment);

m.put(name, sp);
} else
{
studentprofile sp = new studentprofile(...); //<---- whatever constructor is available
sp.setComment(newComment);
m.put(name,sp);
}




Avatar of sigma19

ASKER

HashMap m // <---is exiting HashMap with data

can you clarify me regarding this?
I have no map in my program till now? how should be the declaration
Avatar of sigma19

ASKER

HashMap<String, Object> map = new HashMap<String, Object>();
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
Use the folleoing:

Map<String, studentprofile> map = new HashMap<String, studentprofile>();

Then to access profiles use:


studentprofile profile = map.get(name);
if (profile==null) {
   profile = new studentprofile();
   map.put(name, profile);
}

// now append comment to profile