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
- Constructing a Pair
- AbstractMap.SimpleEntry<K,V> Methods
- Building a Custom Map
- Conclusion
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
and
Methods
Of the six methods, only four will be illustrated in this article. The full syntaxes for the four methods are:
public V getValue()
public V setValue(V value)
and
Constructing a Pair
In Java proper, a pair is a map-entry. The following program uses the first constructor above to construct a pair:
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:
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:
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:
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.
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:
String kvStr = pair.toString();
System.out.println(kvStr);
The output is:
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:
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> 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:
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:
System.out.println(al.get(i).getKey() + " => " + al.get(i).getValue());
}
The output is:
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:
An example for creating a pair object, is:
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:
The following code segment is the creation of a custom map where pairs of the same type, are the elements:
al.add(pair1); al.add(pair2); al.add(pair3); al.add(pair4); al.add(pair4);
Chrys