Arrays.sort(f, (a, b) -> a[0] + a[1] + a[2] - b[0] - b[1] - b[2]);是什么意思?
时间: 2023-12-27 12:02:33 浏览: 29
这是一个使用 Java 8 的 Lambda 表达式(函数式编程)的数组排序方法。该方法将一个二维数组 `f` 按照以下规则进行排序:
- 如果 `a[0] a[1] a[2]` 小于 `b[0] - b[1] - b[2]`,则 `a` 排在 `b` 前面;
- 如果 `a[0] a[1] a[2]` 等于 `b[0] - b[1] - b[2]`,则 `a` 和 `b` 的相对位置不变;
- 如果 `a[0] a[1] a[2]` 大于 `b[0] - b[1] - b[2]`,则 `a` 排在 `b` 后面。
这里的 `a` 和 `b` 分别代表数组 `f` 中的两个元素,它们都是长度为 3 的一维数组。`a[0] a[1] a[2]` 表示将 `a` 数组按顺序拼接成的一个字符串(例如,如果 `a = {1, 2, 3}`,则 `a[0] a[1] a[2]` 的值为 `"123"`)。`b[0] - b[1] - b[2]` 同理。
因此,这个排序方法的目的是按照 `a` 数组拼接成的字符串与 `b` 数组拼接成的字符串的大小关系来排序 `f` 数组。
相关问题
#include<iostream> #include<cmath> #include<algorithm> using namespace std; struct point { int x, y, index; double theta; } p[30000], temp; int f(point& a, point& b) { if (a.theta == b.theta) { if (a.x == b.x && a.y > 0) return a.y * a.y > b.y * b.y; else return a.x * a.x + a.y * a.y < b.x* b.x + b.y * b.y; }return a.theta < b.theta; }int main() { int n; cin >> n; //input for (int i = 0; i < n; i++) { int x, y; cin >> p[i].x >> p[i].y; p[i].index = i + 1; }//exchange min_x to first position int min = p[0].x, minPosition = 0; for (int i = 1; i < n; i++) { minPosition = min <= p[i].x ? minPosition : i; min = min <= p[i].x ? min : p[i].x; }temp = p[0]; p[0] = p[minPosition]; p[minPosition] = temp; //cal atan2 for (int i = 1; i < n; i++) { p[i].x -= p[0].x; p[i].y -= p[0].y; p[i].theta = atan2(p[i].y, p[i].x); }//sort exclude min_x sort(p + 1, p + n, f); //output cout << n << endl; for (int i = 0; i < n; i++) { if (p[i].index == 1) { for (int j = i; j < n; j++) cout << p[j].index << endl; for (int j = 0; j < i; j++) cout << p[j].index << endl; break; } } }转为Java
以下是将给定的 C++ 代码转换为 Java 代码的结果:
```java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class Point {
int x, y, index;
double theta;
public Point(int x, int y, int index, double theta) {
this.x = x;
this.y = y;
this.index = index;
this.theta = theta;
}
}
static Point[] p = new Point[30000];
static int f(Point a, Point b) {
if (a.theta == b.theta) {
if (a.x == b.x && a.y > 0)
return a.y * a.y > b.y * b.y ? 1 : -1;
else
return a.x * a.x + a.y * a.y < b.x * b.x + b.y * b.y ? 1 : -1;
}
return a.theta < b.theta ? 1 : -1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
// input
for (int i = 0; i < n; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
p[i] = new Point(x, y, i + 1, 0);
}
// exchange min_x to first position
int min = p[0].x, minPosition = 0;
for (int i = 1; i < n; i++) {
minPosition = min <= p[i].x ? minPosition : i;
min = min <= p[i].x ? min : p[i].x;
}
Point temp = p[0];
p[0] = p[minPosition];
p[minPosition] = temp;
// cal atan2
for (int i = 1; i < n; i++) {
p[i].x -= p[0].x;
p[i].y -= p[0].y;
p[i].theta = Math.atan2(p[i].y, p[i].x);
}
// sort exclude min_x
Arrays.sort(p, 1, n, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
return f(a, b);
}
});
// output
System.out.println(n);
for (int i = 0; i < n; i++) {
if (p[i].index == 1) {
for (int j = i; j < n; j++)
System.out.println(p[j].index);
for (int j = 0; j < i; j++)
System.out.println(p[j].index);
break;
}
}
scan.close();
}
}
```
private List<String> efJsonToBinList(List<LightGroupReq> lightGroupReqs){ LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_009); ArrayList<String> list = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 48; i++) { frameList.add(new Frames(255, 255, 2550, 2550, 255, 255, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[49]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(255, 255, 2550, 2550, 255, 255, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { list.add(BinaryFileUtils.efFramesToHex(framesList.get(j),framesList.get(j).getGroupIndex(),timeStamp)); } timeStamp++; } return list; }优化此段代码
首先,可以将数组初始化部分的代码优化为使用Java 8的IntStream来进行初始化。
其次,对于portMap的初始化和添加部分,可以使用Java 8的Stream API中的filter和map方法来实现,可以简化代码。
最后,对于framesList的排序部分,可以使用Collections.sort方法代替stream中的sorted方法,因为stream中的sorted方法需要创建一个新的List对象,而Collections.sort方法可以直接对原List进行排序。
优化后的代码如下:
```
private List<String> efJsonToBinList(List<LightGroupReq> lightGroupReqs) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs, VehicleConstants.VEHICLE_009);
List<String> list = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
IntStream.rangeClosed(1, 48).forEach(i -> frameList.add(new Frames(255, 255, 2550, 2550, 255, 255, 0, i, 0, 2)));
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
int[] temp = new int[49];
Arrays.fill(temp, -1);
framesList.forEach(f -> temp[f.getGroupIndex()] = 1);
portMap = IntStream.rangeClosed(1, temp.length - 1)
.filter(i -> temp[i] == -1)
.mapToObj(i -> new Frames(255, 255, 2550, 2550, 255, 255, 0, i, 0, 2))
.collect(Collectors.toMap(Frames::getGroupIndex, f -> f, (a, b) -> a, HashMap::new));
framesList.addAll(portMap.values());
Collections.sort(framesList, Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
framesList.forEach(f -> list.add(BinaryFileUtils.efFramesToHex(f, f.getGroupIndex(), timeStamp)));
timeStamp++;
}
return list;
}
```