In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how java uses Pair to return paired results. It is very detailed and has a certain reference value. Friends who are interested must read it!
Using Pair to realize the return of paired results
In the C _ std::pair + 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 in pairs, which are mainly used to return name-value pairs in Map, such as the Entry class in Map.
When a function needs to return two results, you can use Pair to avoid defining too many data model classes.
The first use is relatively common, and the second use is mainly described here.
1. Define the model class to realize the return of paired results
Function implementation code:
/ * Setter@Getter@ToString@AllArgsConstructorpublic static class PointAndDistance {/ * * Point * / private Point point; / * distance * / private Double distance;} / * * get the closest point and distance * / public static PointAndDistance getNearestPointAndDistance (Point point, Point [] distance) {/ / check point array is empty if (ArrayUtils.isEmpty (points)) {return null } / / get the nearest point and distance Point nearestPoint = points [0]; double nearestDistance = getDistance (point, points [0]); for (int I = 1; I < points.length; iTunes +) {double distance = getDistance (point, point [I]); if (distance < nearestDistance) {nearestDistance = distance; nearestPoint = point [I] }} / / returns the nearest point and distance return new PointAndDistance (nearestPoint, nearestDistance);}
Function use case:
Point point =...; Point [] points =...; PointAndDistance pointAndDistance = getNearestPointAndDistance (point, points); if (Objects.nonNull (pointAndDistance)) {Point point = pointAndDistance.getPoint (); Double distance = pointAndDistance.getDistance ();...} 2. Using Pair class to realize the return of paired results
In JDK, no native Pair data structure is provided, or Map::Entry can be used instead. However, the Pair class in Apache's commons-lang3 package is more useful, which is illustrated by the Pair class below.
Function implementation code:
/ * * get nearest point and distance * / public static Pair getNearestPointAndDistance (Point point, Point [] points) {/ / check point array is empty if (ArrayUtils.isEmpty (points)) {return null;} / / get nearest point and distance Point nearestPoint = points [0]; double nearestDistance = getDistance (point, points [0]); for (int I = 1; I < points.length Double distance +) {double distance = getDistance (point, point [I]); if (distance < nearestDistance) {nearestDistance = distance; nearestPoint = point [I];}} / / returns the nearest point and distance return Pair.of (nearestPoint, nearestDistance);}
Function use case:
Point point =...; Point [] points =...; Pair pair = getNearestPointAndDistance (point, points); if (Objects.nonNull (pair)) {Point point = pair.getLeft (); Double distance = pair.getRight ();.} above is all the content of the article "how java uses Pair to return paired results". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.