Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Pair<T,N> class implementation [closed]

Tags:

java

Is there available a proven Java Pair class implementation?

I mean readily available, widely accepted and tested, maybe part of a more extensive library such as Apache Commons or Guava.

like image 296
yannisf Avatar asked Sep 06 '11 07:09

yannisf


People also ask

How is pair implemented in Java?

The implementation of Pair class contains the following components: Two public fields: first and second, just like in C++. A private constructor: takes two arguments – a key and, its corresponding value. Method of(): A static factory method for creating an immutable, Typed Pair instance.

Why there is no pair in Java?

To sum it up: a generic Pair class doesn't have any special semantics and you could as well need a Tripplet class etc. The developers of Java thus didn't include a generic Pair but suggest to write special classes (which isn't that hard) like Point(x,y) , Range(start, end) or Map.

Does Java have a pair class?

Pair class which can be used to store a pair. We need to store the values into Pair using the parameterized constructor provided by the javafx. util. Pair class.

Is pair in Java mutable?

A mutable pair consists of two object elements. MutablePair is defined in the Apache Commons Lang. The package has to be added to the classpath before MutablePair is used.


2 Answers

Yes, have a look at Apache Commons Pair.

Use sparingly, if at all; left and right doesn't really convey anything about the content or relation between the elements.

(The Pair class was deliberately left out of the standard Java API.)

like image 150
aioobe Avatar answered Oct 11 '22 13:10

aioobe


Map.Entry

What about java.util.Map.Entry interface?

Two concrete implementation bundled with Java 6 and later:

  • java.util.AbstractMap.SimpleEntry
  • java.util.AbstractMap.SimpleImmutableEntry
like image 23
Simon G. Avatar answered Oct 11 '22 14:10

Simon G.