3

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
CQM
  • 42,592
  • 75
  • 224
  • 366

3 Answers3

4

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

Attila
  • 28,265
  • 3
  • 46
  • 55
  • See http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java/156685#156685 for some more explanation. – Louis Wasserman May 23 '12 at 16:10
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)

Community
  • 1
  • 1
bpgergo
  • 15,669
  • 5
  • 44
  • 68
  • 1
    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. – Hunter McMillen May 23 '12 at 15:35
0

You could try a Linked-List of Pair.

Basic structure

(0) --> <-- ((0)(Pair < 1, "Stud" >)(2)) --> <-- (1)(Pair < 2, "Sexy" >)(3)....

Where each Node in the structure is (prev. < K,V >, this < K,V >, next < K,V >) Allows for duplicated keys however...

Head & Tail are null if the Linked-List is empty;