Home > Java > javaTutorial > body text

How to use Pair in java to return paired results

王林
Release: 2023-05-18 19:58:04
forward
1395 people have browsed it

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:

  1. Put key and value together for pair processing, mainly used to return name-value pairs in Map, such as in Map Entry class;

  2. 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 < points.length; i++) {        double distance = getDistance(point, point[i]);        if (distance < nearestDistance) {
            nearestDistance = distance;
            nearestPoint = point[i];
        }
    }    // 返回最近点和距离
    return new PointAndDistance(nearestPoint, nearestDistance);
}
Copy after login

Function usage case:

Point point = ...;
Point[] points = ...;
PointAndDistance pointAndDistance = getNearestPointAndDistance(point, points);if (Objects.nonNull(pointAndDistance)) {
    Point point = pointAndDistance.getPoint();
    Double distance = pointAndDistance.getDistance();
    ...
}
Copy after login

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, Double> 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 < points.length; i++) {        double distance = getDistance(point, point[i]);        if (distance < nearestDistance) {
            nearestDistance = distance;
            nearestPoint = point[i];
        }
    }    // 返回最近点和距离
    return Pair.of(nearestPoint, nearestDistance);
}
Copy after login

Function use case:

Point point = ...;
Point[] points = ...;
Pair<Point, Double> pair = getNearestPointAndDistance(point, points);if (Objects.nonNull(pair)) {
    Point point = pair.getLeft();
    Double distance = pair.getRight();
    ...
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!