簡體   English   中英

在hashMap中排序

[英]Sorting in a hashMap

我在整理此代碼(在哈希圖中)時遇到了一些麻煩-我還需要一些幫助來整理類似的代碼集以獲取double(價格)。 謝謝。

@SuppressWarnings("unused")
public class Inventory {

    Scanner in = new Scanner(System.in);
    ArrayList<Sellable> groceries;
    HashMap<String, Integer> stock;

    public Inventory() {
        groceries = new ArrayList<Sellable>();
        stock = new HashMap<String, Integer>();
//HARDCODING...:
        Sellable n1 = new Produce("Corn", 3, 5.00);
        Sellable n2 = new Snack("Natural Popcorn Seeds", 2.50);
        Sellable n3 = new Produce("Potatoes", 3, 5.00);
        Sellable n4 = new Snack("Organic Potato Chips", 2.50);
        Sellable n5 = new Produce("Apples", 5, 1.75);
        Sellable n6 = new Snack("Apple Juice - 128 oz.", 3.50);
        Sellable n7 = new Produce("Oranges", 5, 1.75);
        Sellable n8 = new Snack("Orange Juice - 128 oz.", 3.50);
//ADD TO HASHMAP
        groceries.add(n1);
        groceries.add(n2);
        groceries.add(n3);
        groceries.add(n4);
        groceries.add(n5);
        groceries.add(n6);
        groceries.add(n7);
        groceries.add(n8);
//PUT UP FOR PRINTING
        stock.put(n1.getName(), 50);
        stock.put(n2.getName(), 100);
        stock.put(n3.getName(), 50);
        stock.put(n4.getName(), 100);
        stock.put(n5.getName(), 50);
        stock.put(n6.getName(), 100);
        stock.put(n7.getName(), 50);
        stock.put(n8.getName(), 100);
    }

//Sorting Method 1
    @SuppressWarnings("unchecked")
    public void sortByName() {
        groceries = new ArrayList<Sellable>();
        stock = new HashMap<String, Integer>();
        {
            if (stock != null) {
                List<Sellable> groceries = new ArrayList<Sellable>();
                stock.addAll(((Entry<String, Integer>) stock).getValue(), groceries);
                Collections.sort(groceries, new Comparator<Sellable>() {
                    public int compare(Sellable product1, Sellable product2) {
                        try {
                            Sellable choice1 = (Sellable) product1;
                            Sellable choice2 = (Sellable) product2;
                            //LESS THAN
                            if (choice1.getName().compareTo(choice2.getName()) < 0) {
                                return -1;
                            } //GREATER THAN
                            else if (choice1.getName().compareTo(choice2.getName()) > 0) {
                                return 1;
                            } //EQUALS
                            else {
                                return 0;
                            }
                        } catch (ClassCastException FAIL) {
                            return -2;
                        }
                    }
                });
            }
        }
    }
}

請刪除代碼行,

groceries = new ArrayList<Sellable>();

stock = new HashMap<String, Integer>();

來自您的方法sortByName().

groceriesstock重置為空。

對不起,但是您的代碼或問題對我來說都沒有多大意義。

  • 您的sortByName破壞了groceriesstock

  • 然后,顯然是試圖從(現在)空的HashMap復制到ArrayList。 但這是荒謬的,因為

    • 您正在嘗試使用不存在的方法( HashMap.addAll ),

    • 您正在將HashMap投射到Entry

    • 等等。

  • Comparator看起來似乎可以按“名稱”排序,但是在ClassCastException事件中返回-2的代碼是錯誤的。 您應該只是讓異常傳播...如果發生。 您實際上並沒有在比較器中進行類型轉換。 參數的類型正確。 (將可Sellable鑄造為可Sellable Sellable ?為什么?)

想添加一個新答案而不是編輯我以前的答案。

請注意,除非確實需要,否則不要(重新)初始化方法中的成員變量。 我也使用String.compareTo()比較名稱。

請在下面找到修改后的排序方法:

    @SuppressWarnings("unchecked")
    public void sortByName() {
//        groceries = new ArrayList<Sellable>();
//        stock = new HashMap<String, Integer>();

            if (stock != null) {
                //List<Sellable> groceries = new ArrayList<Sellable>();
                //stock.addAll(((Entry<String, Integer>) stock).getValue(), groceries);
                Collections.sort(groceries, new Comparator<Sellable>() {
                    public int compare(Sellable product1, Sellable product2) {
                        try {
                            return product1.getName().compareTo(product2.getName());

                        } catch (Exception FAIL) {
                            return -2;
                        }
                    }
                });
            }
        }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM