How to Use Pairs in Java

Jamie Oppel

Updated:

Published:

Java’s “pair” class was introduced in version 7 to create an easy way of returning two values from a method. You can certainly accomplish the same result by writing extra code, though using this existing class will be quicker and easier than implementing your own workaround.

two pawn chess pieces together like pairs in java

The core use of the pair class in Java is to hold a pair of values. This may sound similar to a HashMap, but this just refers to pairs of values that are stored together, not specifically keys mapped to values.

Download Now: An Introduction to JavaScript  [Free Guide]

You might use this class for many reasons, such as working with coordinates or any other circumstance where two return values are needed from a method. Whether it’s working with a graph or something else involving pairs of values, this class has got you covered.

These paired values can then be compared to other paired values to check if they are equal. This means the values in the pairs are compared. Then, a true or false (boolean) result is returned depending on if both pairs of values are the same or not.

This can be extremely useful for any website that does math on a 2d grid, with use cases ranging from calculators to video games. Anything that uses (x,y) coordinates on a 2d plane can gain an advantage from using this class.

Pairs can even help keep track of your mouse on the screen, considering it is nothing more than a point on a 2d plane.

Java Pairs in Action

Let’s go over a few examples of pairs in action, so you can see the concept in action. Take a look at Desmos, a popular website for graphing calculations.

graphing calculator finding the midpoint between two coordinates

Image Source

As both the answer and inputs are a pair of values, the pair class in Java would be perfect for the job. If you wanted to check if the points were equal or to do more complex math than a simple midpoint formula, that could be more easily accomplished with Pairs at the heart of the structure.

With pairs, there is less to keep track of when compared to a workaround like arrays. In the case of arrays as a workaround, you would have to mentally note the index used in the array, or create a definition for that indexed element, which would be more code to write and maintain.

Now you might also be thinking, “Hold on, Desmos uses JavaScript, not Java. What gives?” Well, I have a much more exciting example to show you after this that does indeed use Java. After all, calculators are one of the most boring examples that could have been picked, right? Wrong.

So, it turns out that computers are really nothing more than fancy calculators that can also run video games. Old School Runescape (or OSRS for short) is one particularly popular game that uses a coordinate system and was made with Java.

Clicking on the coordinate grid to move around in Runescape

Image Source

Old School Runescape is a popular online video game that was originally browser-based and written in Java. Support for this game extended well past the release date of Java Version 7 (when pairs were introduced to Java). It’s very likely that this game uses the pairs class in some way every single time you click to walk around or interact with the in-game world.

This is personally my favorite example of Java pairs in action, and it technically was being done on a website up until 2016, when it was then replaced with a standalone client.

How to Use Pairs in Java

If you wanted to revisit that coordinate example from earlier, it would be straightforward to do so. Look at the example code below. This code stores the coordinates (2, 1) and (4, 2) into instances of the pair class. It then takes those values out again to perform some basic math.

Working with coordinates using the Pairs class in Java.

  • First, you will want to import the pairs class with the following line of code. Without this, Java will yell at you, as it doesn’t understand what you’re talking about when you try to reference this class.
  • Next, you'll want to create instances of the pair class using whatever data is necessary. In this case, coordinates could be integers. As the midpoint can also be a decimal number, using the Float data type here is wise to avoid loss of precision during calculations.
  • Grabbing the values from the pairs might cause your eyebrow to raise. Doesn’t this format look very similar to the HashMap data structure?
  • Now, find the midpoint using these coordinates. In plain English, the way to do this is by adding the associated coordinates together, then dividing the result by two.
  • Once the x and y midpoints have been calculated, it’s time to store the answer in a pair object.
  • Finally, display the result in your terminal to ensure the math is accurate. This example should give you a result of 3.0 and 1.5.

Importing the Java Pair class from JavaFX

If you had trouble getting this import to work in VScode, watch this video in order to help get your javaFX environment set up. Once that’s done, this code should work without the Java compiler bullying you.

Creating two coordinates using the Java Pair class

There is a difference here. .getKey() will grab the first value from the pair, and .getValue() will get the second. Remember, these are both just values. There is no real key mapped to a value here.

Grabbing the values from Java Pair class instantiations

So, you will add the x coordinates from each original coordinate together, then divide the result by two to get the x coordinate for the midpoint coordinate. Repeat the process with the y coordinates as well.

Calculating the midpoint of the two coordinates

Storing the resulting midpoint in a Java Pair Class object

Printing the midpoint coordinate to your screen

Result of printing out the midpoint coordinate

Getting Started With Java Pairs

That’s it! Once you get everything set up, using pair in Java is extremely straightforward and even uses methods you are likely already familiar with. Now, start coordinating information with pairs.

New Call-to-action

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about one of the world's most popular programming languages.

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO