Use Pair to return paired results
In C/C language, Pair (pair) is a container that combines two data types into one data type, such as std::pair.
Pair has two main uses:
Put key and value together for pair processing, mainly used to return name-value pairs in Map, such as in Map Entry class;
When a function needs to return two results, you can use Pair to avoid defining too many data model classes.
The first use is more common, here we mainly explain the second use.
1. Define the model class to implement the return of paired results
Function implementation code:
/** 点和距离类 */@Setter@Getter@ToString@AllArgsConstructorpublic static class PointAndDistance { /** 点 */ private Point point; /** 距离 */ private Double distance; }/** 获取最近点和距离 */public static PointAndDistance getNearestPointAndDistance(Point point, Point[] points) { // 检查点数组为空 if (ArrayUtils.isEmpty(points)) { return null; } // 获取最近点和距离 Point nearestPoint = points[0]; double nearestDistance = getDistance(point, points[0]); for (int i = 1; i <p>Function usage case: </p><pre class="brush:php;toolbar:false">Point point = ...; Point[] points = ...; PointAndDistance pointAndDistance = getNearestPointAndDistance(point, points);if (Objects.nonNull(pointAndDistance)) { Point point = pointAndDistance.getPoint(); Double distance = pointAndDistance.getDistance(); ... }
2. Use the Pair class Implementing the return of paired results
In the JDK, the native Pair data structure is not provided, and Map::Entry can be used instead. However, the Pair class in Apache's commons-lang3 package is easier to use. The following uses the Pair class as an example.
Function implementation code:
/** 获取最近点和距离 */public static Pair<point> getNearestPointAndDistance(Point point, Point[] points) { // 检查点数组为空 if (ArrayUtils.isEmpty(points)) { return null; } // 获取最近点和距离 Point nearestPoint = points[0]; double nearestDistance = getDistance(point, points[0]); for (int i = 1; i <p>Function use case: </p><pre class="brush:php;toolbar:false">Point point = ...; Point[] points = ...; Pair<point> pair = getNearestPointAndDistance(point, points);if (Objects.nonNull(pair)) { Point point = pair.getLeft(); Double distance = pair.getRight(); ... }</point>
The above is the detailed content of How to use Pair in java to return paired results. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools