Java HashMap创建,编辑和删除

时间:2012-02-12 21:30:54

标签: java iterator hashmap

我正在尝试更新HashMap直接在下一个方法中使用它,但它无效。从我读到的,我找不到解决方案。有人说这是不可能的,有人说使用迭代器,但即使使用迭代器它也无法正常工作。错误是打印方法它不打印甚至进入while循环,因为它是空的但我不知道为什么

这是我尝试更新和打印一些信息的两种方法。

  import java.io.File;
     import java.util.ArrayList;
     import java.util.HashMap;
     import java.util.Iterator;
     import java.util.Scanner;
     import java.util.Enumeration;
     import java.util.Hashtable;
     import java.util.Iterator;
     import java.util.Map;
     import java.util.Set;

    public class OrderList {
    // Storage for an arbitrary number of details.

    private HashMap<String, Order> orderList = new HashMap<String, Order>();

    /**
     * Perform any initialization .
     */
    public OrderList() {
        orderList = new HashMap<String, Order>();
    }

    public HashMap<String, Order> getOrders() {
        return orderList;

    }

    public void readOrderFile(String OrderListPath) {
        try {
            File file = new File(OrderListPath);
            Scanner scan = new Scanner(file);
            while (scan.hasNextLine()) {
                String readLine = scan.nextLine();

                if (readLine != null) {

                    getSplitLinesOrders(readLine);
                }
            }

        } catch (Exception e) {
        }
    }

    public void getSplitLinesOrders(String readLine) {
        String id = "";
        String customerId = "";
        String itemId = "";
        int quantity = 0;
        try {
            String[] splitedLine = readLine.split(",");

            if (splitedLine.length == 4) {
                id = splitedLine[0];
                customerId = splitedLine[1];
                itemId = splitedLine[2];
                quantity = Integer.parseInt(splitedLine[3]);
                Order newOrder = new Order(id, customerId, itemId, quantity);
                orderList.put(id, newOrder);
            }
        } catch (Exception e) {
        }


    }

    /**
     * Add a new set of details to the list
     * @param details The details of the staff
     */
//    public void addDetails(Order details) {
//        orderList.add(details);
//    }
    public boolean hasOrder() {
        return orderList.size() != 0;
    }

    public Order getNextOrder() {
        Order order = orderList.remove(0);
        return order;

    }

    /**
     * @return All the details
     */
    public String listDetails() {
        StringBuffer allEntries = new StringBuffer();
        for (Map.Entry<String, Order> details : orderList.entrySet()) {
            String Key = details.getKey();
            Object value = details.getValue();
            allEntries.append(Key + " " + value);
        }
        return allEntries.toString();
    }

    public void PrintListOfOrders() {
        Iterator it = getOrders().entrySet().iterator();
        try {

            while (it.hasNext()) {
                Order value = (Order) it.next();
                System.out.println(value.getOrderId() + " " + value.getCustomerId() + " " +     value.getItemId() + " " + value.getQuantity());
            }




        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你可能得到NullPointerException?下次告诉我们出了什么问题并提供堆栈跟踪(如果适用)。

您发布的代码不会创建orderList的实例,因此如果在其他地方没有这样做,代码会抛出NullPointerException

尝试添加:

 private HashMap<String, Order> orderList = new HashMap<String, Order>;

像这样吞下Exception

} catch (Exception e) {
}

不是一个好习惯,因为它会隐藏所有关于出错的信息,至少可以:

catch (Exception e) {
   e.printStacktrace();

}

答案 1 :(得分:0)

你可以这样做:

Set<String> s = List.keySet();
Iterator<String> i = s.iterator();

这是迭代器的初始化,然后你可以使用i.next()遍历密钥,每次都获取一个String并要求orderList.get(thatString)获取值。