2017-04-09 47 views
-2

公共類汽車實現可比{我如何排序Java中的ArrayBlockingQueue?

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; 
} 

}

公共類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()),集合.sort(queue),doesn; t wok;這是爲明天的演示文稿....有人請welp

+1

首先,標題中沒有必要使用這種語言。其次,我們不知道「welp」是什麼意思。第三,ArrayBlockingQueue是FIFO結構,並不意味着排序。 –

回答

0

明顯排序ArrayBlockingQueue將無法​​正常工作,因爲它會違背其FIFO設計。如果你想分類Queue,那麼你應該使用PriorityQueue

+0

也嘗試過(BlockingQueue隊列= new PriorityBlockingQueue ....),didn; t排序任何東西... –

+0

@AndreiBratu你定義了它的泛型類型來使用你的'Cars'類嗎? 'BlockingQueue queue = new PriorityBlockingQueue <>();' –

+0

你好,我剛剛從數組改變爲優先級....但這裏是我應該做的:所以我的Cars類應該實現hashcode,equals和compareTo才能爲排序,並在隊列類我應該使用PriorityBlockingQueue 並使用Collections.sort或Arrays.sort然後它完成? –

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); 
    } 
} 
+0

感謝您的提示;我不想使用汽車數組,但是使用BlockingQueue,因爲它是來自併發包的數據結構,我需要它作爲線程中的一個示例用於我的演示:D –

+0

如果您不想使用['ArrayBlockingQueue '](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html),您應該使用['PriorityBlockingQueue']的有序性質(https:// docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue。html),但是請注意,它僅針對*檢索*元素,而不是*迭代*它們:*所提供的'Iterator'不能保證以任何特定順序遍歷'PriorityBlockingQueue'的元素。* – Andreas

+0

... *如果您需要有序遍歷,請考慮使用'Arrays.sort(pq.toArray())**(請參閱此答案)**。此外,可以使用'drainTo'方法按優先順序刪除部分或全部元素,並將它們放入另一個集合中。* – Andreas