2

in my scenario the hashTable is like this

AId=1
BId=1
catalogId=10053
reason_1=RET-KP
reason_2=RET-KP
quantity_1=1.0
ItemId_1=468504
quantity_2=1.0
ItemId_2=468505

Now i need to delete all _i things when reason_i=RET-KP
ie. delete ItemId_1 & quantity_1 Where reason_i is reason_1,reason_2

So how i can iterate this hashTable and delete the keys(dynamic) based on their values and storing it in hashTable again.

  • possible duplicate of [Iterating hashtable in java](http://stackoverflow.com/questions/2351331/iterating-hashtable-in-java) – Bucket Mar 21 '14 at 13:26
  • 2
    You should rethink your design instead. Storing String keys with a numeric suffix in a map like this is not something you should do. It just looks like you should have a `List`, where Item would be a class containing an ID, a catalogId, a reason, a quantity, etc. Java is an OO language. Use objects. – JB Nizet Mar 21 '14 at 13:27

1 Answers1

0

Check this will solve your problem .

 package com.loknath.lab;



 public class HashTableDemo {
public static void main(String args[]) {

    Hashtable htable = new Hashtable(3);
    boolean deleteStatus;
    ArrayList<String> list = new ArrayList<String>();
    // populate the table
    htable.put("AId", 1);
    htable.put(" catalogId", 2);
    htable.put(" ItemId_1", 43);
    htable.put("ItemId_2", 43);
    htable.put("bid", 54.45);

    Set<String> keys = htable.keySet();

    for (String key : keys) {

        System.out.println(key);
        deleteStatus = check(key);
        if (deleteStatus) {
            list.add(key);
        }
    }

    for (String string : list) {
        htable.remove(string);
    }

}

public static boolean check(String key) {
    boolean status = false;
    status = key.contains("_");
    return status;
}

 }
loknath
  • 1,362
  • 16
  • 25
  • here you are checking with the condition when key contains `_` but i need to delete these dynamic keys based on their values. – user3046880 Mar 23 '14 at 03:38