sigma19
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.
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.
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);
-------------------------- ---------- ------
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);
--------------------------
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....
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);
}
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);
}
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
can you clarify me regarding this?
I have no map in my program till now? how should be the declaration
ASKER
HashMap<String, Object> map = new HashMap<String, Object>();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
--------------------------
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);
--------------------------