我有以下Supplier
類型的對象列表,我想用reverseOrder()
方法對它們進行排序(所以它們將按降序排列)。但是,在互聯網上閱讀整整一天後,我仍然無法實現這一目標。我敢肯定,這是我在這裏失蹤的真正小事。按升序排列的自然順序工作得很好。使用reverseOrder()與自定義比較器以降序排序列表?
這裏是我Supplier
類:
public class Supplier {
private String supplierName = "";
private String representative = "";
private String representativesPhoneNumber = "";
private Map<Drug, Integer> listOfDrugs = new HashMap<Drug, Integer>();
Supplier(String n, String rep, String repPhoneNum, String drugName, double drugPrice, int stock) {
this.supplierName = n;
this.representative = rep;
this.representativesPhoneNumber = repPhoneNum;
listOfDrugs.put(new Drug(drugName, drugPrice), stock);
}
public Map<Drug, Integer> getListOfDrugs() {
return this.listOfDrugs;
}
public static Integer getKeyExtractor(Supplier supplier, Drug drug) {
return Optional.ofNullable(Optional.ofNullable(supplier.getListOfDrugs())
.orElseThrow(() -> new IllegalArgumentException("drugs is null")).get(drug))
.orElseThrow(() -> new IllegalArgumentException("the drug couldn't be found"));
}
}
它有一個Map
如果對象<Drug, Integer>
。 這裏是我Drug
類:
public class Drug {
private String name = "";
private double price = 0.0;
Drug(String n, double p) {
this.name = n;
this.price = p;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp^(temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Drug other = (Drug) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
}
大部分代碼是修剪,垃圾郵件的緣故。 :)
而且我Orders
班,在那裏我居然做了排序:
public class Orders {
private Map <Drug, Integer> orderedDrugs = new HashMap <Drug, Integer>();
private Vector<Supplier> suppliers = new Vector <Supplier>();
public void sort(Drug drug, List<Supplier> sortedSuppliers) {
Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug), Comparator.reverseOrder()));
}
public List<Supplier> getSortedSuppliersByQuantity(Drug drug) {
List <Supplier> sortedSuppliers = new ArrayList <Supplier>();
for(Supplier s : suppliers) {
for(Entry<Drug, Integer> entry : s.getListOfDrugs().entrySet()) {
if(entry.getKey().getDrugsName().equals(drug.getDrugsName()));
sortedSuppliers.add(s);
}
}
sort(drug, sortedSuppliers);
return sortedSuppliers;
}
}
的代碼重新修整,只顯示爲實際問題所需要的方法。
所以我到目前爲止已經有嘗試:
Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug), Comparator.reverseOrder()));
Collections.sort(suppliers, Collections.reverseOrder(Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug))));
但兩者不工作。我需要在某個地方實施compareTo()
還是我缺少某種方法?由於升序工作,但不下降。
與Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug)));
一起按升序排列並工作。
非常感謝您的幫助,對於這篇長文章感到抱歉!
UPDATE:
我也試圖在Supplier
類來實現compareTo
,但我得到一個NPE。 :/
public int compareTo(Supplier a) {
for(Entry<Drug, Integer> entry : listOfDrugs.entrySet()) {
int result = listOfDrugs.get(entry.getKey()).compareTo(a.listOfDrugs.get(entry.getKey()));
if(result != 0)
return result;
}
return 0;
}
你是什麼意思「都不行」? –
@NicolasFilotto,我的意思是,當我嘗試使用它們時,我以原始方式得到List - 無序。 – Calihog
請提供一個示例,以便我們輕鬆複製 –