?
Solved

NEWBIE: How to implement HashMap with triplets?

Posted on 2005-04-14
12
Medium Priority
?
533 Views
Last Modified: 2012-05-05
Dear Experts,

What is the best way to store BOTH a String and an Integer as the "value" part of a key/value pair?  I want to use a HashMap to store the following:

the Key String name:  "Bob"

WITH a Value String description: "My Friend"
AND a Value Integer age: 37

How do I do that?

Thanks,
BrianMc1958
 
0
Comment
Question by:BrianMc1958
  • 5
  • 4
  • 3
12 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13781487
I could see 2 ways of doing this:

1) join them together in a String separated by a certain character which you can then break/split later
e.g.
aHashMap.put( "bob" , "My Friend:37") ;

2)create a custom object that has 2 fields, status and age, and then add this. This can then be retrived later and cast back to the original object and the data taken out. This is the better approach.

Person aPerson = new Person() ;
aPerson.setStatus("My Friend");
aPerson.setAge(37);
aHashMap.put( "bob" , aPerson) ;
0
 

Author Comment

by:BrianMc1958
ID: 13781552
Dear bloodredsun,

Thanks.  I had been thinking along those lines myself.  However, I should have mentioned that retrieval speed is important.  (Loading speed is not.)  Given that HashMap is already significantly slower than simple arrays, I was concerned about  further speed degradation with custom objects.  

But I'm a newbie.  Is retrieval speed likely to be affected much by a custom object?  

Thanks,
BrianMc1958
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 400 total points
ID: 13781719
Yes, instantiation of objects is commonly one of the things that takes time. Here's a quick example of the above code in action so you can play with it. The first thing you could do is change the constructor so that it uses the variables themselves rather than using the object methods.

Let me have a bit of a play and I'll get back to you...

>>But I'm a newbie
To Java, maybe, but what other languages do you know?

Person.java
---------------------
package com.bloodredsun.examples;

import java.util.HashMap;

public class Person {
      
      public Person(){}
      
      public Person(String pStatus, int pAge){
            setStatus( pStatus ) ;
            setAge( pAge ) ;
      }
      
      private String status ;
      private int age ;

      public String getStatus() {
            return status;
      }

      public void setStatus(String pStatus) {
            status = pStatus;
      }

      public int getAge() {
            return age;
      }

      public void setAge(int pAge) {
            age = pAge;
      }
      
      public static void main(String[] args){
            //create map
            HashMap aHashMap = new HashMap();
            //create new Person and add
            Person aPerson = new Person() ;
            aPerson.setStatus("My Friend");
            aPerson.setAge(37);
            aHashMap.put( "bob" , aPerson) ;
            //shorter version of above
            aHashMap.put( "bert" , new Person("my enemy" , 59)) ;
            
            //get bob out of the Hashmap remembering to cast
            //back to Person from Object
            Person bobPerson = (Person) aHashMap.get("bob");
            //output values for bob
            System.out.println( "bobs values: ") ;
            System.out.println( "status: " + bobPerson.getStatus() );
            System.out.println( "age: " + bobPerson.getAge() );
      }
}
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 35

Assisted Solution

by:TimYates
TimYates earned 100 total points
ID: 13781752
>> Given that HashMap is already significantly slower than simple arrays, I was concerned about  further speed degradation with custom objects.  

THis is only true if you are going to be iterating over the whole map...

For retrieval of a specific item in the Map, it will out-pace arrays...

Make sure you have the right structure for the job...

iteration->List
selection->Map
0
 

Author Comment

by:BrianMc1958
ID: 13781975
To bloodredsun:

I've said this before but... I'm still stunned that people will take the time to actually write a little (working) program to answer a stranger's question.  So thanks.

>>To Java, maybe, but what other languages do you know?
I do know Pascal (from way back in school), which has a native object (I forget the name) that would handle this situation.

To Everybody:

OK.  Maybe my problem has been answered.  To summarize what I think I've learned:

1. HashMap with custom object as the Value is a (if not the) standard way to handle associating a key with multiple values.
2. Instantiation may be slow but...
3. Retrieval of a specific item (which is what I care about) is actually faster.

Is that right?

Thanks again,
BrianMc1958



0
 
LVL 35

Expert Comment

by:TimYates
ID: 13782000
>> 2. Instantiation may be slow but...

Iteration, not instantiation

Other than that, yes, you are right :-)
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13782161
Yep,

.....as per usual I just get to confirm what Tim said is correct ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13782184
hehehe, usually it's the other way round ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13782193
It must be a sign that my CAB parser is driving me up the wall ;-)

stoopid rubbish format...
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13782209
hahaha! I wish...
0
 

Author Comment

by:BrianMc1958
ID: 13782907

Thanks once again, folks!

--BrianMc1958
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13782930
Good luck!

:-)
0

Featured Post

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.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 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