Created
April 12, 2019 17:46
-
-
Save nvmnghia/6486f5d2e9b45431a759a131626cc34b to your computer and use it in GitHub Desktop.
Convert to ArrayList by passing to its constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
class Student implements Comparable<Student> { | |
private int id; | |
private String name; | |
private double cgpa; | |
public Student(String name, double cgpa, int id) { | |
this.id = id; | |
this.name = name; | |
this.cgpa = cgpa; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getCgpa() { | |
return cgpa; | |
} | |
@Override | |
public int compareTo(Student o) { | |
if (this.cgpa != o.cgpa) { | |
return this.cgpa > o.cgpa ? -1 : 1; | |
} | |
int compareName = this.name.compareTo(o.name); | |
if (compareName != 0) { | |
return compareName; | |
} | |
return this.id - o.id; | |
} | |
} | |
class Priorities { | |
public List<Student> getStudents(List<String> events) { | |
PriorityQueue<Student> pQueue = new PriorityQueue<>(); | |
for (String event : events) { | |
if (event.charAt(0) == 'E') { | |
String[] ops = event.split(" "); | |
pQueue.offer(new Student(ops[1], Double.parseDouble(ops[2]), Integer.parseInt(ops[3]))); | |
} else { | |
pQueue.poll(); | |
} | |
} | |
return new ArrayList(pQueue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment