比较器声明语法
Comparator Declaration Syntax
我正在使用以下代码在 Java 中实现优先级队列和比较器接口,并按照以下建议使用 Java 8:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.PriorityQueue;
class Student {
private int token;
private String name;
private double cgpa;
public Student(int id, String name, double cgpa) {
super();
this.token = id;
this.name = name;
this.cgpa = cgpa;}
public int getToken() {
return token;}
public String getName() {
return name;}
public double getCgpa() {
return cgpa;}
}
class Priorities {
public List < Student > getStudents(List <String> events) {
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
List <Student> students = new ArrayList <Student> ();
for (String e: events) {
Scanner in =new Scanner(e);
String event = in.next();
if (event.equals("ENTER")) {
String name = in.next();
float cgpa = in.nextFloat();
int token = in.nextInt();
Student student = new Student(token, name, cgpa);
student_queue.add(student);
} else if (event.equals("SERVED")) {
Student first = student_queue.poll();
} in .close();
}
Student first = student_queue.poll();
if (first == null) {
return students;
} else {
while (first != null) {
students.add(first);
first = student_queue.poll();
}
return students;
}
}
}
public class Solution {
private final static Scanner scan = new Scanner(System.in);
private final static Priorities priorities = new Priorities();
public static void main(String[] args) {
int totalEvents = Integer.parseInt(scan.nextLine());
List<String> events = new ArrayList<>();
while (totalEvents-- != 0) {
String event = scan.nextLine();
events.add(event);
}
List<Student> students = priorities.getStudents(events);
if (students.isEmpty()) {
System.out.println("EMPTY");
} else {
for (Student st: students) {
System.out.println(st.getName());
}
}
}
}
我收到以下错误,但无法解决问题。
Solution.java:30: 错误: 找不到符号
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
^
符号:变量比较器
位置:class 优先事项
Solution.java:30: 错误:无效的方法引用
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
^
无法从静态上下文中引用非静态方法 getCgpa()
Solution.java:30: 错误:无效的方法引用
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
^
无法从静态上下文中引用非静态方法 getName()
Solution.java:30: 错误:无效的方法引用
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
^
无法从静态上下文中引用非静态方法 getToken()
注意:Solution.java 使用未经检查或不安全的操作。
注意:使用 -Xlint:unchecked 重新编译以获取详细信息。
4 个错误
我能找到的唯一错误是您忘记了 PriorityQueue
的 import
。
import java.util.PriorityQueue;
除此之外,剪切并粘贴到我的 IDE 编译正常。
"Java" 在 Hackerrank 上的意思是 Java 7.
您需要选择 "Java 8" 以获得对 Student::getCpa
语法的支持。
https://www.hackerrank.com/environment/languages
另请注意,当前(长期支持的)Java 版本是 Java 11。因此,在使用较新的结构或 API 时要小心,它们可能在Java 8 个。
我正在使用以下代码在 Java 中实现优先级队列和比较器接口,并按照以下建议使用 Java 8:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.PriorityQueue;
class Student {
private int token;
private String name;
private double cgpa;
public Student(int id, String name, double cgpa) {
super();
this.token = id;
this.name = name;
this.cgpa = cgpa;}
public int getToken() {
return token;}
public String getName() {
return name;}
public double getCgpa() {
return cgpa;}
}
class Priorities {
public List < Student > getStudents(List <String> events) {
PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken));
List <Student> students = new ArrayList <Student> ();
for (String e: events) {
Scanner in =new Scanner(e);
String event = in.next();
if (event.equals("ENTER")) {
String name = in.next();
float cgpa = in.nextFloat();
int token = in.nextInt();
Student student = new Student(token, name, cgpa);
student_queue.add(student);
} else if (event.equals("SERVED")) {
Student first = student_queue.poll();
} in .close();
}
Student first = student_queue.poll();
if (first == null) {
return students;
} else {
while (first != null) {
students.add(first);
first = student_queue.poll();
}
return students;
}
}
}
public class Solution {
private final static Scanner scan = new Scanner(System.in);
private final static Priorities priorities = new Priorities();
public static void main(String[] args) {
int totalEvents = Integer.parseInt(scan.nextLine());
List<String> events = new ArrayList<>();
while (totalEvents-- != 0) {
String event = scan.nextLine();
events.add(event);
}
List<Student> students = priorities.getStudents(events);
if (students.isEmpty()) {
System.out.println("EMPTY");
} else {
for (Student st: students) {
System.out.println(st.getName());
}
}
}
}
我收到以下错误,但无法解决问题。
Solution.java:30: 错误: 找不到符号 PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken)); ^ 符号:变量比较器 位置:class 优先事项 Solution.java:30: 错误:无效的方法引用 PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken)); ^ 无法从静态上下文中引用非静态方法 getCgpa() Solution.java:30: 错误:无效的方法引用 PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken)); ^ 无法从静态上下文中引用非静态方法 getName() Solution.java:30: 错误:无效的方法引用 PriorityQueue < Student > student_queue = new PriorityQueue(Comparator.comparing(Student::getCgpa).reversed().thenComparing(Student::getName).thenComparing(Student::getToken)); ^ 无法从静态上下文中引用非静态方法 getToken() 注意:Solution.java 使用未经检查或不安全的操作。 注意:使用 -Xlint:unchecked 重新编译以获取详细信息。 4 个错误
我能找到的唯一错误是您忘记了 PriorityQueue
的 import
。
import java.util.PriorityQueue;
除此之外,剪切并粘贴到我的 IDE 编译正常。
"Java" 在 Hackerrank 上的意思是 Java 7.
您需要选择 "Java 8" 以获得对 Student::getCpa
语法的支持。
https://www.hackerrank.com/environment/languages
另请注意,当前(长期支持的)Java 版本是 Java 11。因此,在使用较新的结构或 API 时要小心,它们可能在Java 8 个。