Discuss / Java / PriorityQueue,银行柜台叫号

PriorityQueue,银行柜台叫号

Topic source

净净一隅

#1 Created at ... [Delete] [Delete and Lock User]

package Queue;

import java.util.Comparator;

import java.util.PriorityQueue;

import java.util.Queue;

import static tools.LogUtils.log;

public class PriorityQueueTest {

public static void main(String[] args) {

   Queue<User> q = new PriorityQueue<>();

   q.offer(new User("Anna","A1"));

   q.offer(new User("Bob","A10"));

   q.offer(new User("Jack","A2"));

   q.offer(new User("Tom","V1"));

   q.offer(new User("Tina","V2"));

   log.info(q.poll());

   log.info(q.poll());

   log.info(q.poll());

   log.info(q.poll());

   log.info(q.poll());

   log.info(q.poll());   

   }

   class User implements Comparable<User>{

   private String name;

   private String number;

   public User(String name,String number) {

   this.name=name;

   this.number=number;

   }

   public String toString() {

   return this.name+":"+this.number;    

   }

   public int compareTo(User other) {

   if(this.number.charAt(0)==other.number.charAt(0)) {

   return Integer.parseInt(this.number.substring(1))-Integer.parseInt(other.number.substring(1));   

   }

   if(this.number.charAt(0)=='V')

   {

   return -1;

   }else {

   return 1;

   }

   }

   }


  • 1

Reply