Avatar of MJ
MJFlag for United States of America asked on

Create a Look up/Hash table to produce multiple variable assignments

What I'm looking for is the best way to take a sting value and based on that, perform a lookup on that value and assign 4 other variables values. What's the best approach/method?

JavaJSP

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
humanonomics

Hi,

Can you give a small example for elaborating more on what are you trying to achieve ?

Thanks.
SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
MJ

CEHJ... given your example, how would I access the "d" value to assign to A string LETTER?

      

Map<String, X> m = new HashMap<String, X>();
m.put("a", new X("b", "c", "d", "e"));
ASKER
MJ

Hi humanonomics... picture a switch statement functionality but more elegant.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CEHJ

>>CEHJ... given your example, how would I access the "d" value to assign to A string LETTER?

You would use the accessor method of whatever field it had been assigned to. Let's call it 'Three'

X x = map.get("a");
String s = x.getThree(); // returns 'd'
ASKER
MJ

Sorry I'm not a Java person at all! So I'm assuming I need a method, as in your example we would call it Three? I'm sorry but I'm not following how this returns the third value?
Mick Barry

You would get the third value using:

Object third = map.get(s)[2];

Java indexes start at 0 so third index would be 2.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

And to assign a value u would use:

map.get(s)[2] = "A";

ASKER
MJ

Sorry Objects... so are you saying just use

map.put(s)[2] = "A"; to assign and Object third = map.get(s)[2]; to retrieve and not use your initial solution?
Mick Barry

thats using what I originally suggested but showing how to access and set values

maps.get("X")  gets *all* the values assigned to "X"
maps.get("X")[0] gets the first value assigned to "X"
maps.get("X")[1] gets the second value assigned to "X"
etc.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
MJ

Objects,
Just to clarify:

Map<String, Object[]> map = new HashMap<String, Object[]>();

map.put("myKey", new Object[] { "cat", "dog", "fish" });

maps.get("myKey")[0] gets "cat"
Mick Barry

thats correct
humanonomics

But then that could lead to big time, class cast exception if not handled properly, in this case, example given by CHEJ is best suit. I will go with that, that is more fail safe then using an Object array.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

What CEHJ is just a minor variation of what I suggested.
You didn't state what the values were so I just kept it generic, you would of course adjust what you store in the map to match the values you actually store.
eg. if they are all strings then use an array of strings.

Mick Barry

What values do you want to store?

humanonomics

True I agree, if all "894359" wants to store is Strings or all the variables fo same datatype, then the solution given by you will work fine @ objects. When the values are of different data types then the solution provided by CHEJ will have to be implemented . :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Mick Barry

Also depends on how many variables you want to store, and whether the number of variables is static or dynamic. So without know the details of the variables theres not much point getting into details and second guessing requirements.
Let me know exactly what you want to store and I'll see what fits best.

CEHJ

>>What CEHJ is just a minor variation of what I suggested.

No - it's a completely different paradigm. Java is an object-oriented language and where appropriate (usually), data that belong together should be encapsulated in classes
ASKER
MJ

They will all be strings!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
MJ

How about I split the points then!  :)
ASKER
MJ

Thanks to all! Much appreciated and thanks for helping out someone that knows very little about Java
CEHJ

>>They will all be strings!

That doesn't really matter much. The point is that any grouping of related attributes in an object-oriented language such as Java should normally be in a class, unless there's a good reason they shouldn't be
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Mick Barry

then use:

Map<String, String[]> map = new HashMap<String, String[]>();

No casting required then :)