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*

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
9
  • 10
    I like static factory method of. It reminds of Google Guava immutable collections.
    Jarek Przygódzki
    –  Jarek Przygódzki
    2011-04-05 19:35:16 +00:00
    Commented Apr 5, 2011 at 19:35
  • 7
    You are at some point casting o1 to Comparable, even though nothing indicates it will actually implement that interface. If that is a requirement, the FIRST type parameter should be FIRST extends Comparable<?>.
    G_H
    –  G_H
    2011-04-20 17:36:49 +00:00
    Commented Apr 20, 2011 at 17:36
  • I'm not a java guy, so please forgive me for my ignorance, but what sort of helper classes were you thinking of in the TODO comments?
    user220878
    –  user220878
    2012-03-29 14:41:28 +00:00
    Commented Mar 29, 2012 at 14:41
  • 3
    31 is a bad constant for hashCode. For example, if you use HashMap keyed by Pair<Integer,Integer> for 2D map, you will get many collisions. For example (a*65497)^b would be better suited.
    Michał Zieliński
    –  Michał Zieliński
    2013-06-29 18:58:16 +00:00
    Commented Jun 29, 2013 at 18:58
  • 1
    @MarioCarneiro ^ is xor, not power
    Michał Zieliński
    –  Michał Zieliński
    2016-01-13 21:03:15 +00:00
    Commented Jan 13, 2016 at 21:03

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