?
Solved

hashmap

Posted on 2011-04-26
8
Medium Priority
?
440 Views
Last Modified: 2012-05-11
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.

0
Comment
Question by:vkchaitu82
  • 4
  • 3
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35470107

-------------------------------------------------
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35470124
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
 

Author Comment

by:vkchaitu82
ID: 35470175
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Expert Comment

by:for_yan
ID: 35470227
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
 

Author Comment

by:vkchaitu82
ID: 35470372
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
 

Author Comment

by:vkchaitu82
ID: 35470385
HashMap<String, Object> map = new HashMap<String, Object>();
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 35470469
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
 
LVL 92

Expert Comment

by:objects
ID: 35472124
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question