Java

Pair Class for Java and Not for JavaFX

The following are a list of fruit names and their outer colors:

blackberry => dark blue-black

banana => yellow

watermelon => green

grape => pink

strawberry => red

Each line is a pair of key/value. The name of a fruit is the key, and its color is its value. It is possible to have another list where the keys are numbers or the values are numbers, or both keys and values are numbers. In the above list, the keys are strings and the values are strings.

JavaFX is a Java library that is not installed with Java. It has a class called Pair, to be used for handling pairs. The problem with using the Pair class with Java, is that the programmer still has to install JavaFX onto Java, and learn how to use JavaFX.

This problem can be avoided by using the entry feature in Java proper. Pairs are called map-entries (or simply entries) in Java proper. This article explains how to handle entries in Java, as opposed to how to handle pairs in JavaFX, with all the problems in using JavaFX. You end up with the same results. Entries are the key/value pairs in Java. The equivalent of the Pair class in JavaFX is the AbstractMap.SimpleEntry<K,V> class in Java, which is explored in this article. The class AbstractMap.SimpleEntry<K,V> is in the java.util.* package, which has to be imported.

Article Content

Class AbstractMap.SimpleEntry<K,V> Overview

This class creates a pair. A pair is code having a key and its corresponding value.

Constructors

This class has two constructors and six methods. The full syntaxes for the constructors are

public SimpleEntry(K key, V value)

and

public SimpleEntry(Map.Entry<? extends K,? extends V> entry)

Methods

Of the six methods, only four will be illustrated in this article. The full syntaxes for the four methods are:

public K getKey()

public V getValue()

public V setValue(V value)

and

public String toString()

Constructing a Pair

In Java proper, a pair is a map-entry. The following program uses the first constructor above to construct a pair:

import java.util.*;

public class TheClass {

public static void main(String[] args) {

AbstractMap.SimpleEntry<String, String> pair = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

}

}

The type of the key is String, and the type of the value is also String. The following program shows how a pair is constructed from another pair, using the second constructor above:

import java.util.*;

public class TheClass {

public static void main(String[] args) {

AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>(pair1);

}

}

The key/value of pair2 is “blackberry”/”dark blue-black”, which is same as that of pair1.

AbstractMap.SimpleEntry<K,V> Methods

Out of the six methods, only four will be illustrated here.

public K getKey()

After creating a pair, its key can be returned, as shown in the following code segment:

AbstractMap.SimpleEntry<String, String> pair = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

String kStr = pair.getKey();

System.out.println(kStr);

In this case, the return type is a string and it is: blackberry.

public V getValue()

After creating a pair, its value can be returned as shown in the following code segment:

AbstractMap.SimpleEntry<String, String> pair = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

String vStr = pair.getValue();

System.out.println(vStr);

In this case, the return type is a string and it is: “dark blue-black”.

public V setValue(V value)

The value of a pair can be set, but its key cannot be set. In the following code segment, the value of the pair is changed.

AbstractMap.SimpleEntry<String, String> pair = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

pair.setValue("light green-white");

public String toString()

This returns a string representation of the key and value of the pair. The following code illustrates this:

AbstractMap.SimpleEntry<String, String> pair = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

String kvStr = pair.toString();

System.out.println(kvStr);

The output is:

blackberry=dark blue-black

where = separates the key from the value.

Building a Custom Map

The map-entry (pair) is not really meant to be used with Hashtable or HashMap or similar map data structure. It is actually meant to be used to create a custom map data structure. It is easy to create a custom map data structure: just look for a list data structure such as the ArrayList, whose members (fields and methods) are of interest; and let pairs be the elements of the list.

The syntax to create an empty ArrayList object is:

ArrayList<T> al = new ArrayList<T>();

where al is the ArrayList object. The syntax to add an element (pair) to an ArrayList, is

al.add(E e)

A Java main() method with a custom map called al, can have the following pairs:

AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

AbstractMap.SimpleEntry<String, String> pair2 = new AbstractMap.SimpleEntry<String, String>("banana", "yellow");

AbstractMap.SimpleEntry<String, String> pair3 = new AbstractMap.SimpleEntry<String, String>("watermelon", "green");

AbstractMap.SimpleEntry<String, String> pair4 = new AbstractMap.SimpleEntry<String, String>("grape", "pink");

AbstractMap.SimpleEntry<String, String> pair5 = new AbstractMap.SimpleEntry<String, String>("strawberry", "red");

The code to create the ArrayList object and add the pairs would be:

ArrayList<AbstractMap.SimpleEntry<String, String>> al = new ArrayList<AbstractMap.SimpleEntry<String, String>>();

al.add(pair1); al.add(pair2); al.add(pair3); al.add(pair4); al.add(pair5);

The code to print the key/value pairs of the custom map, would be:

for (int i=0; i<al.size(); i++) {

System.out.println(al.get(i).getKey() + " => " + al.get(i).getValue());

}

The output is:

blackberry => dark blue-black

banana => yellow

watermelon => green

grape => pink

strawberry => red

Conclusion

A pair, called map-entry in Java proper, is code having a key and its corresponding value. The syntaxes to construct a pair are:

public SimpleEntry(K key, V value)

public SimpleEntry(Map.Entry<? extends K,? extends V> entry)

An example for creating a pair object, is:

AbstractMap.SimpleEntry<String, String> pair1 = new AbstractMap.SimpleEntry<String, String>("blackberry", "dark blue-black");

The class AbstractMap.SimpleEntry<K,V> for creating a pair is in the java.util.* package, which has to be imported. The AbstractMap.SimpleEntry class has methods. The syntaxes for four of which, are:

public K getKey()

public V getValue()

public V setValue(V value)

public String toString()

The following code segment is the creation of a custom map where pairs of the same type, are the elements:

ArrayList<AbstractMap.SimpleEntry<String, String>> al = new ArrayList<AbstractMap.SimpleEntry<String, String>>();

al.add(pair1); al.add(pair2); al.add(pair3); al.add(pair4); al.add(pair4);

Chrys

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.