简体   繁体   English

如何从HashMap检索以前的值?

[英]How to retrieve previous values from HashMap?

I have the following code where I'm reading from a text file. 我从文本文件中读取以下代码。 The text file i as follows: 文本文件i如下:

111 Laptop 500 10
222 Mobile 120 8
333 Notebook 4 100
444 Chocolates 3 50
555 Guitar 199 5
666 LenovoLaptop 470 10
777 HPLaptop 450 10
888 SonyVAIO 525 5

If the user enters ID as 111 , the following should be the output: 如果用户输入ID111 ,则应输出以下内容:

111 Laptop 500 10
666 LenovoLaptop 470 10
777 HPLaptop 450 10
888 SonyVAIO 525 5

I'm storing the the contents of the text file in a HashMap. 我将文本文件的内容存储在HashMap中。 Here is the code: 这是代码:

public void comparableItems(String ID)
{
    File f = new File("C://Class/items.txt");
    HashMap<String, Item> map = new HashMap<String, Item>();

    try
    {
        Scanner scan = new Scanner(f);

        while(scan.hasNext())
        {
            String line = scan.nextLine();
            String temp[] = line.split(" ");

            Item it = new Item(temp[0], temp[1], Double.parseDouble(temp[2]), Integer.parseInt(temp[3]));

            map.put(it.itemID, it);
        }
        if(map.containsKey(ID))
        {
            Item item = map.get(ID);
            if(item.price>=item.price+100 && item.price<=item.price-100)
            {

            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Here is the Item class: 这是Item类:

public class Item 
{
String itemID;
String itemName;
double price;
int quantity;

public Item(String itemID, String itemName, double price, int quantity)
{
    this.itemID = itemID;
    this.itemName = itemName;
    this.price = price;
    this.quantity = quantity;
}

public void printItemDetails()
{
    System.out.println("ID\tItemName\tUnitPrice\tQuantity");
    System.out.println("===================================================");

    System.out.println(this.itemID+ "\t" +this.itemName+ "\t" +this.price+ "\t"+this.quantity);
}
}

How do I get the desired output? 如何获得所需的输出? I'm in the learning stages of Java Collections. 我正处于Java Collections的学习阶段。 So please bear with me. 所以,请忍受我。

Can someone help me here? 有人可以帮我吗?

Thanks! 谢谢!

Your Map isn't doing you much good. 您的Map您没有多大帮助。 Since you know what reference item ID you're looking for before you even parse the file, you could just capture that item when (and if) you see it during the parse. 由于您甚至在解析文件之前就已经知道要查找的参考项目ID,因此您可以在解析期间(以及是否)看到该项目时捕获该项目。 You do need some kind of collection to hold all the other items, but a List would be a better choice for this particular task. 您确实需要某种集合来容纳所有其他项目,但是对于此特定任务, List是一个更好的选择。

In any case, the thrust of your question seems to be about examining all the other items you parse from the file. 无论如何,问题的重点似乎在于检查从文件中解析出的所有其他项目。 For this, you want to iterate over the Map 's values() view (and to get your comparisons right): 为此,您想遍历Mapvalues()视图(并正确进行比较):

for (Item otherItem : map.values()) {
    if((otherItem.price <= item.price + 100)
            && (otherItem.price >= item.price - 100)) {
        otherItem.printItemDetails();
    }
}

If you collected the items in a List instead of a Map , then you would replace map.values() in the above with just list (or whatever name you use for the List ). 如果您将项目收集在List而不是Map ,则可以将上面的map.values()替换为list (或List使用的任何名称)。

For what you say you want (items with prices near the desired item), a HashMap isn't an efficient datastore. 对于您想要的内容(价格接近所需商品的商品),HashMap并不是有效的数据存储。

However, since it sounds like this is your homework, once you use map.get("111") to get your laptop, get the price P, and then iterate over the hashmap to get any item whose price is within your desired delta of P. The Collections tutorial tells you how to iterate over a collection. 但是,由于这听起来像是您的功课,因此一旦您使用map.get(“ 111”)来获取笔记本电脑,请获取价格P,然后遍历哈希图以获取价格在您期望的delta范围内的任何商品。 P.集合教程告诉您如何遍历集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM