简体   繁体   中英

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:

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:

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. 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:

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. So please bear with me.

Can someone help me here?

Thanks!

Your Map isn't doing you much good. 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. 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.

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):

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 ).

For what you say you want (items with prices near the desired item), a HashMap isn't an efficient datastore.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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