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)

Share:
13,642
CQM
Author by

CQM

Updated on June 04, 2022

Comments

  • CQM
    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
      James Cronen over 11 years
      Looks like a standard implementation of a Tuple...
    • Hunter McMillen
      Hunter McMillen over 11 years
      There is no built in object to support this like Pair in C++, but it would be relatively easy to create one.
    • cadrian
      cadrian over 11 years
    • trutheality
      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
      trutheality over 11 years
      @CQM Just wondering whether you actually want Collections.singletonMap(...)
  • Hunter McMillen
    Hunter McMillen over 11 years
    That 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
    Louis Wasserman over 11 years
    See stackoverflow.com/questions/156275/… for some more explanation.