2016-11-07 50 views
0

我有以下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; 
    } 
} 

的代碼重新修整,只顯示爲實際問題所需要的方法。

所以我到目前爲止已經有嘗試:

  1. Collections.sort(suppliers, Comparator.comparing(s -> Supplier.getKeyExtractor(s, drug), Comparator.reverseOrder()));

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

你是什麼意思「都不行」? –

+0

@NicolasFilotto,我的意思是,當我嘗試使用它們時,我以原始方式得到List - 無序。 – Calihog

+1

請提供一個示例,以便我們輕鬆複製 –

回答

1

嘗試

Collections.sort(suppliers, 
       Comparator.comparing((Supplier s) -> Supplier.getKeyExtractor(s, drug)).reversed()); 

我建了一個簡化的版本,這個工作。我沒有和你一起嘗試Supplier等課程。

+0

Reverse只反轉列表。在下面看到我的更新。 –

+1

@RavindraHV'比較器#顛倒'與任何列表無關。 [閱讀javadocs](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#reversed--) - 它返回一個比較器,強制該比較器的反向排序。 – bradimus

+0

感謝您的幫助@bradimus,但它不適合我。 :(我得到和原來一樣的列表 – Calihog

-2

是的,你需要。執行Comparator(並根據需要對其進行調整),並調用sort方法。

更新: 要使用lambda表達式進行操作,請嘗試使用here

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

對於可能看起來沒有問題的東西我很抱歉,但我認爲'Comparator.comparing(s - > Supplier .getKeyExtractor(s,drug),Comparator.reverseOrder())'實際上是Java-8語法中的比較器嗎? – Calihog

+0

已使用新鏈接更新答案。反向排序反轉列表。它與降序排序不同。我是lambda新手,但我可以告訴你的是,你需要實現compare-to並定製邏輯 - 更大,更等,更小。 –

+0

更新:顛倒訂單**確實會顛倒訂單!我正在查看[reverse-list](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#reverse-java.util.List-)。 –

0

好的,我找到了一個解決問題的辦法,因爲一切都不適合我。在按升序對列表進行排序後,使用sort方法,我只需調用:Collections.reverse(myList);,然後按降序獲取排序列表。我知道這可能是蹩腳的,但它適用於我。