In this article, we discussed a very useful programming concept called Pair. Pairing provides a convenient way to handle simple key-value associations, especially useful when we want to return two values from a method.
is available in the core Java library using the implementation of Pair. In addition, some third-party libraries, such as Apache Commons and Vavr, have exposed this functionality in their respective apis.
core Java pairing implementation
Pair class h3>
In the javafx.util package, the class constructor takes two arguments, the key and the corresponding value:
Pair<Integer, String> pair = new Pair<>(1, "One"); Integer key = pair.getKey(); String value = pair.getValue();
example describes a simple Integer to String mapping using the Pair class. In the example, the getKey method returns the key object, and the getValue method returns the corresponding value object.
AbstractMap. SimpleEntry and AbstractMap. SimpleImmutableEntry h3>
SimpleEntry is defined in the abstract class AbstractMap and is constructed in a similar way to Pair:
AbstractMap.SimpleEntry<Integer, String> entry = new AbstractMap.SimpleEntry<>(1, "one"); Integer key = entry.getKey(); String value = entry.getValue();
has keys and values that can be obtained using standard getter and setter methods.
The AbstractMap class also contains a nested class that represents an immutable pairing: the SimpleImmutableEntry class.
AbstractMap.SimpleImmutableEntry<Integer, String> entry = new AbstractMap.SimpleImmutableEntry<>(1, "one");
applications with variable pairing, in addition to the configuration value cannot be modified, try to modify will throw an UnsupportedOperationException anomalies.
the Apache Commons library, org.apache.com mons. Lang3. The tuple package provides Pair abstract class, cannot be instantiated directly.
has two subclasses, which represent mutable and immutable pairs: ImmutablePair and MutablePair. Both implement access to key/value and setter and getter methods:
ImmutablePair<Integer, String> pair = new ImmutablePair<>(2, "Two"); Integer key = pair.getKey(); String value = pair.getValue();
try in ImmutablePair setValue method, will throw an UnsupportedOperationException anomalies. But execution on variable pairings is perfectly normal:
Pair<Integer, String> pair = new MutablePair<>(3, "Three"); pair.setValue("New Three");
h2> Vavr library
The immutable Tuple2 class in the
Vavr library provides pairing:
Tuple2<Integer, String> pair = new Tuple2<>(4, "Four"); Integer key = pair._1(); String value = pair._2();
in this implementation, an object cannot be modified after it has been created, so the update method returns a new instance after the change:
tuplePair = pair.update2("New Four");
this article discusses the Java pairing concept and shows examples of its different implementations, core Java and third-party provided implementations.
- C + + programming fault handling — error: assignment of read only data member ‘STD:: pair
- Map to vector pair map.second sort
- After introducing security, the service cannot be registered with Eureka
- Springboot: start error reporting after introducing paging plug-in PageHelper
- Introducing external configuration files into spring XML db.properties
- Feignexception $unauthorized is reported by introducing security call service
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- After introducing sass into Vue project, start to report error typeerror [err]_ INVALID_ ARG_ Type]: the “path” argument must be of type string
- zookeeper Failed to Startup: Error: JAVA_HOME is not set and java could not be found in PATH
- Error: (44,73) lambda expression and XML are not supported in Java: – source 1.7 Error:java : Compilation failed
- Problem in configuring Maven error: Java_ HOME not found in your environment.
- Syntax error, insert “Finally” to complete TryStatem in Myeclipse in Java-
- The solution to “polling news feeds” appeared in Eclipse in Java
- There is no getter for property named ‘id‘ in ‘class java.lang.Integer‘
- On the intern () method of string class in Java
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Compare whether two sets are the same in Java
- How many pieces of data can list store in Java?
- The difference between sleep() and wait() in Java