Issue
I use jdk11 in eclipse and there is no library for pair? or I need to add external jars?
Solution
Add OpenJFX libraries to your project
OpenJFX, the open-source implementation of JavaFX hosted on the OpenJDK project, is not bundled with many of the builds of OpenJDK supplied by some vendors.
You must choose one of these three solutions:
- Download the OpenJFX libraries and manually add to your project
- Use a dependency & build tool such as Maven or Gradle to download and install he OoenJFX libraries.
- Obtain your build of OpenJDK from a vendor that chooses to bundle the OpenJFX library. The only one I know of is Liberica from BellSoft.
Other “Pair” solutions
If you are not building JavaFX gui apps, and just want a class to hold a pair of values like a key-value, then adding JavaFX implementation to get javafx.util.Pair
is overkill. Seek an alternative.
One alternative is to write your own Pair
class. Simple enough.
Another alternative is to use the Map.Entry
interface built into Java. Two implementations are built in, one mutable and one immutable.
As the name suggests, the original purpose of this class is to hold the key and value pair as an entry in a Map
implementation object. But some folks use this interface and classes separately, without a map.
Example. Let’s track who is working on a particular day-of-week.
AbstractMap.SimpleImmutableEntry< DayOfWeek , String > dayWorker =
new SimpleImmutableEntry<>(
DayOfWeek.WEDNESDAY ,
"Alice"
)
;
DayOfWeek dow = dayWorker.getKey() ; // DayOfWeek.WEDNESDAY
String worker = dayWorker.getValue() ; // "Alice"
Answered By - Basil Bourque
Answer Checked By - Clifford M. (JavaFixing Volunteer)