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.

vkchaitu82Asked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
Your task setting kind of implied that you had such table - beacuse you
were saying that people will entaer name, comment and you need to update
comment.

If you imagine some web application - normnally
at the satrt of the servlet you would read data say from database
and at that time you would create you HashMap - before your query you'll cretae
empty
HashMap m = new HashMap();

then youll run query - retriebve data - names, comments, etc
for each row you'll probably create the studsentprofile object
studentprofile sp = new studentprofile(name);
sp.setComment(...)
m.put(name, sp);

when you are done with this initial collection of
data then usually when your prohgram
will reaxch the state of some additions,
so by that time you already have at least partially poplated
hashmap.
nevertheles our code would work even
for the ver first tome when
efefctively you have yet not items in hashmap
It will just use only that branch of if,
where get() method returns null
0
 
for_yanCommented:

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

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



0
 
for_yanCommented:
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);

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


0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
vkchaitu82Author Commented:
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....
0
 
for_yanCommented:
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);
}




0
 
vkchaitu82Author Commented:
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
0
 
vkchaitu82Author Commented:
HashMap<String, Object> map = new HashMap<String, Object>();
0
 
objectsCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.