Java HashMap 创建、编辑和删除

发布于 2025-01-04 11:52:40 字数 3222 浏览 1 评论 0原文

我正在尝试更新 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);
        }
    }
}

I am trying to update a HashMap use it directly in the next method, but it isn't working. From what I read I couldn't find a solution. Some say'd it is impossible and some say use an iterator, but even with the iterator it's not working. the error is the printing method it is not printing or even getting inside the while loop because it is empty but i cant find why

This is the two methods I'm trying to update and print some information.

  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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2025-01-11 11:52:40

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

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

尝试添加:

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

吞咽 Exception 像这样:

} catch (Exception e) {
}

不是一个好的做法,因为它会隐藏有关出现问题的所有信息,至少这样做:

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

}

You're probably getting a NullPointerException? Next time tell us what is going wrong and provide stacktraces if applicable.

The code you posted doesn't create an instance of orderList, so if it's not done elsewhere that code will throw a NullPointerException

Try adding:

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

Swallowing an Exception like this:

} catch (Exception e) {
}

is not a good practice since it will hide all information about what's going wrong, at least do:

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

}

罗罗贝儿 2025-01-11 11:52:40

你可以这样做:

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

这是迭代器的初始化,然后你可以使用 i.next() 遍历键,每次获取一个字符串并询问 orderList.get (thatString) 来获取值。

You could do something like this:

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

Which is the initialization of the iterator, and then you could iterate through the keys using i.next(), getting a String each time and asking orderList.get(thatString) to get the value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文