Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

What is the equivalent of the C++ Pair<L,R> in Java?

Is there a good reason why there is no Pair<L,R> in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.

It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry<K,V>), but this looks quite convoluted.

Answer*

Cancel
4
  • 26
    The Java-mockery would have been fine if you had pointed to the Apache Commong Lang, which contains a Pair class.
    haylem
    –  haylem
    2012-02-29 09:10:22 +00:00
    Commented Feb 29, 2012 at 9:10
  • 6
    Or you could just use SimpleImmutableEntry
    CurtainDog
    –  CurtainDog
    2012-06-28 01:40:22 +00:00
    Commented Jun 28, 2012 at 1:40
  • The most common IDEs will generate an appropriate HashCode()/equals for you.
    java-addict301
    –  java-addict301
    2017-11-13 15:12:51 +00:00
    Commented Nov 13, 2017 at 15:12
  • 4
    I agree. "Because this is Java" is a good answer. Remember that the Java language is intentionally deprived of certain (cough c++) features that are confusing to the average programmer. That is why Java does not allow you to redefine operators. Nor does it allow multiple inheritance. Bottom line is, if some stupid programmer is likely to abuse it, then Java will make it hard to do.
    John Henckel
    –  John Henckel
    2017-11-15 15:01:12 +00:00
    Commented Nov 15, 2017 at 15:01

Morty Proxy This is a proxified and sanitized view of the page, visit original site.