我如何在java中对ArrayBlockingQueue进行排序?

时间:2017-04-09 21:17:33

标签: java

公共类汽车实施可比较{

private String company, model;
private int price;


public Cars(String company, String model, int price){
    this.company=company;
    this.model=model;
    this.price=price;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + price;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Cars other = (Cars) obj;
    if (price != other.price)
        return false;
    return true;
}

public String toString(){
    return String.format("Comapany: %s, Model: %s, Price: %d", this.company, this.model, this.price);
}

public String getCompany() {
    return company;
}


public void setCompany(String company) {
    this.company = company;
}


public String getModel() {
    return model;
}


public void setModel(String model) {
    this.model = model;
}


public int getPrice() {
    return price;
}


public void setPrice(int price) {
    this.price = price;
}

public int compareTo(Object obj) {


    return this.price-((Cars)obj).price;
}

}

public class BlockingQueueExample {

BlockingQueue<Cars> queue;
Random random=new Random();
private boolean running=false;

public BlockingQueueExample(BlockingQueue<Cars> queue){
    this.queue=queue;
    running=true;
}

public void producer() throws InterruptedException{

    while(running){
        int add_car=random.nextInt(5);
        Cars value=null;
        switch(add_car){
        case 0:value=new Cars("BMV", "q7", 4000);break;
        case 1:value=new Cars("Renault","KWID", 2000);break;
        case 2:value=new Cars("Porche","Cayenee", 3000);break;
        case 3:value=new Cars("Skoda", "Rapid", 2500);break;
        case 4:value=new Cars("Volkswagen", "Ameo", 3500);break;
        }

        queue.put(value);
        System.out.println("PRODUCER "+ value);
        System.out.println();
    }
}

public void consumer() throws InterruptedException{

    while(running){
        Thread.sleep(500);
        if(random.nextInt(5)==0){
            Cars value=queue.take();
            //Collections.sort((List<Cars>) queue);
            System.out.println("CONSUMER Taken value: "+value +", Queue size: "+queue.size()+"\n"+queue);
            System.out.println();
        }
    }
}

public void stop(){
    running=false;
    // method to sort queue
    System.out.println("Sorted queue:"+"\n"+queue);
}

}

我尝试过Arrays.sort(queue.toArray()),Collections.sort(queue),没有尝试过;这是明天的演示....有人请写一下

2 个答案:

答案 0 :(得分:0)

显然,对ArrayBlockingQueue进行排序是行不通的,因为它会违反其FIFO设计。如果您想要排序Queue,那么您应该使用PriorityQueue

答案 1 :(得分:0)

您无法对BlockingQueue进行排序,但可以对元素数组进行排序。

你的Arrays.sort(queue.toArray())尝试几乎是正确的。您只需要记住数组并打印它,而不是未排序的队列。

Cars[] arr = queue.toArray(new Cars[queue.size()]);
Arrays.sort(arr);
System.out.println("Sorted elements:\n" + Arrays.toString(arr));

相依

您不应使用原始通用Comparable。将其更改为Comparable<Cars>

此外,减去整数值以产生compare()值是容易出错的(数值溢出)。请改用Integer.compare()

public class Cars implements Comparable<Cars> {

    // lots of code

    @Override
    public int compareTo(Cars other) {
        return Integer.compare(this.price, other.price);
    }
}