我有以下Supplier
类型的对象列表,我想使用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)));
将它们按升序排序并工作。
提前感谢您的帮助,很抱歉发了这么长的帖子!
更新:我也试图在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;
}
Try
Collections.sort(suppliers,
Comparator.comparing((Supplier s) -> Supplier.getKeyExtractor(s, drug)).reversed());
我建立了一个简化的版本,这工作。我没有尝试与您Supplier
等类
好的,我已经找到了一个解决问题的方法,因为其他所有方法都不适合我。在我使用sort
方法按升序对列表进行排序之后,我只需调用:Collections.reverse(myList);
,就可以按降序获得排序后的列表。我知道这可能很蹩脚,但它对我很有用。
是的,你需要。实现Comparator(并根据需要进行调整),并调用sort方法。
更新:要使用lambda表达式执行此操作,请尝试如下所示。
更新# 2:下面是我想到的。希望能有所帮助:
/**
Input:[9, 9, 5, 1, 6, 3, 9, 4, 7, 1]
Reversed:[1, 7, 4, 9, 3, 6, 1, 5, 9, 9]
ReverseOrdered:[9, 9, 9, 7, 6, 5, 4, 3, 1, 1]
*/
private static void testCollectionsSort() {
List<Integer> integerList = new ArrayList<>();
int size=10;
Random random = new Random();
for(int i=0;i<size;i++) {
integerList.add(random.nextInt(size));
}
System.out.println("Input:"+integerList);
List<Integer> integerListTwo = new ArrayList<>(integerList);
Collections.reverse(integerListTwo);
System.out.println("Reversed:"+integerListTwo);
Comparator<Integer> integerComparator = (Integer a, Integer b) -> b.compareTo(a); // 'b' is compared to 'a' to enable reverse
Collections.sort(integerList, integerComparator);
System.out.println("ReverseOrdered:"+integerList);
}