Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple parameter-value pairs in Java?

Tags:

java

I want to make simple parameter-value pairs of the form (SomeClass, SomeNumber) where the 1st thing is a parameter and second thing is its value. SomeNumber can be a whole number, fraction, negative number etc. SomeClass can be String, Color or anything that can be paired with "SomeNumber" .

ex of employee and salaries. (John, 19.75), (David, 25.50), (Cathy, 102.50) ex for color codes (Color.black, 0), (Color.blue, 2) etc.

Is it a good idea? If not why? If yes, then how do I do it in Java?

like image 910
SuperStar Avatar asked Aug 27 '26 11:08

SuperStar


1 Answers

What you are talking about is Map

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Try HashMap

Map<String,Double> myMap = new HashMap<String,Double>();
      |       |     
      |       |
SomeClass  SomeNumber

myMap.put("John", 19.75); 

SomeNumber num =  myMap.get("John");
like image 50
Karthik T Avatar answered Aug 30 '26 01:08

Karthik T