0

I'm translating some code from standard C++ to Java. Several places utilize

std::pair<T1,T2> 

more specifically

std::pair<Date,double>

Is there a standard idiomatic way to express a parameterized pair type in Java? As one example, I'm having to use a custom type

class Observation {
   LocalDate date;
   double value;
}

I have other cases as well where two or three things need to be returned from a function. In other languages, I would likely use pairs, triples, and other tuple entities.

I do not really mind making the needed little classes, but would like to know if I'm somewhere missing out on a Java feature.

Thanks in advance for your comments.

(P.S. I'm pretty sure that this is my first question ever posed on S.O.)

John Griffin
  • 377
  • 2
  • 8
  • Thanks. I do believe that somehow I missed that question, and it is definitely the same question. That said, I disagree with that answer that making little classes rather than semantics-hiding pair or other tuples is a good thing. Still, thank you very much and it does totally and correctly answer my question. – John Griffin Oct 12 '16 at 21:29
  • Java very deliberately leaves this out. The point is for you to make a proper class with usefully named fields. – Louis Wasserman Oct 12 '16 at 22:56
  • I understand the intention of the Java designers to force me to make a class with two named fields, and that when in Rome, one should do as the Romans do. I just wanted to ensure that I was in fact doing as the Romans do. I disagree with that deliberate forcing and if it were to be correct, one must agree that those from outside Rome, for instance C++, Go, Scheme, Scala, Haskell, and Python programmers, much be following a poor language design decision when utilizing pairs or tuples. Given that Java has now lambdas, I won't be surprised to see tuples one day. – John Griffin Oct 13 '16 at 00:29
  • No, that doesn't follow. What's a good choice for one language doesn't have to be a good choice for others. Pairs may be appropriate for one language but not another. (And the Java leadership still seems *actively* opposed to pairs.) – Louis Wasserman Oct 13 '16 at 00:30
  • @LouisWasserman Really? I miss how unnamed structures are reasonable in one but not another. Maybe it's the Rome thing, really. When in C++, I -feel- as though the std::pair works just fine in localized use, just as it feels okay in Python. I'm not being coy, even if it sounded a bit, I just don't see that unnamed structures are inherently less valid than named ones. Of course, it's moot, because that's not what Java does. Anyway, it looks as though I'm causing a slightly inappropriate discussion, so I'll sign off. Thanks for the comments very much. – John Griffin Oct 13 '16 at 00:35

1 Answers1

1

There's nothing in the standard Java API, but various third-party libraries have pair implementations. For example. this one in Apache Commons Lang.

Todd Dunlap
  • 141
  • 6
  • Thanks very much as well. This is a viable thing as well as simply implementing a simple pair class myself, but I'll just be doing the standard Java "make a little class" instead of having a dependency. – John Griffin Oct 12 '16 at 21:33