STL Pair equivalent in Java 5.0?

I’m trying to figure out why there’s no templated Pair class in Java 5, you know, like the STL Pair. Of course I’ve written my own but it would be nice to use the Sun implemented version if one exists. Am I missing something blatantly obvious here?

Underlying the collection generics (not templates… Java generics are NOT C++ templates… I know, disappointing); when using “pairs” the implementation uses Map.Entry<K,V>. It’s kinda like a Pair.

I know that Java generics are not like C++ templates, that was my poor choice of wording there. As for using Map as a pair that doesn’t really work if you want to be able to access either value in the map directly (ie with an index). For example I’m mapping spatials to game objects for my event notification system and so I have an

ArrayList<Pair<Spatial, GameObject>>

That way when I get the index of the pair I'm dealing with I just say ArrayList.get(i).first or .second. This is of course with my own implementation of Pair which is pretty trivial to do, still I'm dissapointed it's not in java.util.

ArrayList<Map.Entry<Spatial, GameObject>> _list;

Would work pretty much the same way.

Spatial spatial = _list.get[0].getKey();
GameObject obj = _list.get[0].getValue();

But, I'm sure that your Pair is much more elegant. :D

Hmm, never realized you could use a Map that way, guess I should read the JavaDoc more carefully :// I think I’ll stick to my Pair though for the forseeable future, thanks for the input!

Just for me to learn: is there any aspect in Pair that is not covered by the "normal" Map in Java? (note: this is NO language flamewar start, just my curiosity, since I do not use C++ very often).

Pair doesn’t use the map interface, in fact it’s basically just a templated struct (do you say ‘genericized class’ for a Java class that is using generics). This is how you would implement a pair in Java using generics (from memory).

class Pair<T, S>
   public T first;
   public S second;
   Pair(T first, S second)
      this.first = first;
      this.second = second;

Really it's just a convenient wrapper for whatever types you want to throw in. It can be very useful for situations where you want to return two things but don't need a "heavyweight" solution like a Map or a specialized class to wrap the return values in. Since you can directly access the members it's very lightweight in terms of access cost but you give up knowing for sure what "first" is like you would know for a Map with a key.

I just want to make sure that it’s understood… The above class is ‘Map.Entry’, not ‘Map’. I’m sure everyone caught that, but just wanted to point it out to limit confusion.

Also, Map.Entry is actually an Interface, not an implemented class. So thus, much more usefull in this contect. Although, I think it might make sense to have Pair implement Map.Entry. So, from that standpoint my example above is false and E.Moniker is very much in the right in saying that it’s kinda dumb that there isn’t a Pair already in the API.

You’re right guurk, Pair could implement Map.Entry without too much trouble, but Entry is still set up with the mindset of using a key to find a value (ie not having a setValue but not setKey). On a slightly different topic, do you know of a structure that acts like a map but can use either value as the key? I implemented my own using two hashmaps and an arraylist and it works pretty well I just wanted to know if a structure existed.