3 Replies - 6575 Views - Last Post: 17 November 2010 - 11:14 AM Rate Topic: ****- 1 Votes

#1 javastarkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-November 10

Passing objects as values to Hashmap

Posted 17 November 2010 - 10:39 AM

Hello,

I am working on creating some Hashmaps in different ways. I am stuck at one place now. I assigned some values to an object and pass that object as value to Hashmap with the key as student. And not knowing how to retrieve those values.

Please Help!

Here is the code I wrote:

StudentHashMap stud=new StudentHashMap();//creating class object
		
		stud.firstName="Santhosh";//Assigning values to object
		stud.lastName="Nellutla";
		stud.rollNumber=1;
		
		HashMap studentMap=new HashMap();// creating HashMap object
		studentMap.put("Student", stud);
		
		stud.studentDetails(studentMap);



Is This A Good Question/Topic? 0
  • +

Replies To: Passing objects as values to Hashmap

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Passing objects as values to Hashmap

Posted 17 November 2010 - 10:44 AM

See the HashMap get() method. Also, HashMaps are generic, as are all the other Java Collections.

Why are you creating a custom HashMap here? Or is this just a general Student? If this is just a Student, the class naming conventions are misleading.
StudentHashMap stud=new StudentHashMap();//creating class object 


Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Passing objects as values to Hashmap

Posted 17 November 2010 - 10:55 AM

Assuming Student.rollNumber is unique, you'd need HashMap<Integer, Student>
Was This Post Helpful? 1
  • +
  • -

#4 javastarkid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-November 10

Re: Passing objects as values to Hashmap

Posted 17 November 2010 - 11:14 AM

View Postmacosxnerd101, on 17 November 2010 - 09:44 AM, said:

See the HashMap get() method. Also, HashMaps are generic, as are all the other Java Collections.

Why are you creating a custom HashMap here? Or is this just a general Student? If this is just a Student, the class naming conventions are misleading.
StudentHashMap stud=new StudentHashMap();//creating class object 




Firstly,Thank you.

Yes I did that too. Here is the code I wrote:


private void studentDetails(HashMap studentMap)// same method I tried to call in the previous code 
	{
		String value = (String)studentMap.get("Student");
		System.out.println(value);
		System.out.println();
		
		/*Set keys = map.keySet();
		Iterator keysIte = keys.iterator();
		
		while(keysIte.hasNext())
		{			
			String key = (String)keysIte.next();
			System.out.println("Book key = "+key+"     And value = "+(String)map.get(key));
		}*/





It is executing without any error but the o/p is :

null
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1