java key/value pair object, that isn't an array/map
13,642
Solution 1
You will need to create your own. This is done (AFAIK) to facilitate a better semantic meaning from the class name, than a general Pair<>
or KeyValuePair<>
would
Solution 2
http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
and an implementation from SO Java - How to create new Entry (key, value)
Author by
CQM
Updated on June 04, 2022Comments
-
CQM over 1 year
I want a key value pair object. Just two strings
so
Object<String, String>
is there an existing object in java to facilitate this before I reinvent the wheel?
The only reason I didn't immediately gravitate to Hash/Map this time is because that one object is made to store multiple key/value pairs. Is there one designed to hold just a single key/value pair, that I can turn into an array if I feel inclined to do so?
-
James Cronen over 11 yearsLooks like a standard implementation of a Tuple...
-
Hunter McMillen over 11 yearsThere is no built in object to support this like
Pair
in C++, but it would be relatively easy to create one. -
cadrian over 11 yearsalready answered: stackoverflow.com/questions/156275/…
-
trutheality over 11 years"that I can turn into an array if I feel inclined to do so?" -- can you clarify? Do you want
Pair( str1, str2 ).toArray() --> [str1, str2]
do you actually want a single-entry map? -
trutheality over 11 years@CQM Just wondering whether you actually want
Collections.singletonMap(...)
-
-
Hunter McMillen over 11 yearsThat class sort of implies that this class will be used as a part of something, with a bunch of these
Entry
objects. The OP just wants a stand alone object that can hold two things. -
Louis Wasserman over 11 yearsSee stackoverflow.com/questions/156275/… for some more explanation.