public class Main {
public static void main(String
[] args
) {
Queue
<User> q
= new PriorityQueue<>(new Comparator<User>() {
public int compare(User u1
, User u2
) {
if (u1
.number
.charAt(0) == u2
.number
.charAt(0)) {
return u1
.number
.compareTo(u2
.number
);
}
if (u1
.number
.charAt(0) == 'V') {
return -1;
} else {
return 1;
}
}
});
q
.offer(new User("Bob", "A1"));
q
.offer(new User("Alice", "A2"));
q
.offer(new User("Boss", "V1"));
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
}
}
class User {
public final String name
;
public final String number
;
public User(String name
, String number
) {
this.name
= name
;
this.number
= number
;
}
public String
toString() {
return name
+ "/" + number
;
}
}
public class Main {
public static void main(String
[] args
) {
Queue
<User> q
= new PriorityQueue<>(new UserComparator());
q
.offer(new User("Bob", "A1"));
q
.offer(new User("Alice", "A2"));
q
.offer(new User("Boss", "V1"));
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
System
.out
.println(q
.poll());
}
}
class UserComparator implements Comparator<User> {
public int compare(User u1
, User u2
) {
if (u1
.number
.charAt(0) == u2
.number
.charAt(0)) {
return u1
.number
.compareTo(u2
.number
);
}
if (u1
.number
.charAt(0) == 'V') {
return -1;
} else {
return 1;
}
}
}
class User {
public final String name
;
public final String number
;
public User(String name
, String number
) {
this.name
= name
;
this.number
= number
;
}
public String
toString() {
return name
+ "/" + number
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-17054.html