0

Hi i have to validate the database values with the UI displayed values so I am using Watij as an automation tool. The problem arises when i have to store the database values into the hashmap. Suppose the database is having 3 fields Name, Email and Address. And after firing the query the fetched rows are 10. I am taking the field values as the Key in hashmap and the retrieved rows as the values.

I am unable to store the values in hashmap. When i used the hashmap the values got overridden and at last i always got the single values for the respective keys. I tried declaring the hashmap having two parameters as string and an string[] but i was unable to read the final values. Can anybody help as i am not a java expert. Thanks.

Maxim Dsouza
  • 1,507
  • 3
  • 19
  • 32
Bridge
  • 11
  • 1
  • 2
  • A little code would be helpful. Can't get the clear picture what is your problem here. – Talha Ahmed Khan Jun 14 '11 at 07:48
  • 3
    Another case of [object denial](http://stackoverflow.com/questions/3725703/how-to-store-more-than-one-string-in-a-map). You're approaching the problem from the wrong end. – Joachim Sauer Jun 14 '11 at 07:57
  • I've noticed on SO lately that the Map seems to be _the_ most misused and misunderstood data structure in Java. Object denial indeed! – Adriaan Koster Jun 14 '11 at 09:47

4 Answers4

1

Where have you declared the string? You will have to declare it inside the loop(where you loop through your resultset). If you are not creating a new object inside the loop, the reference of the same string would be stored in all the values of the hashmap and you would get a single value in your hashmap. If the code was also mentioned here, it would be easy to pinpoint the exact problem.

Maxim Dsouza
  • 1,507
  • 3
  • 19
  • 32
1

Well in a map, the keys need to be unique, so if you keep add (email,blabla@hotmail.com) and it already had (email,nana@gmail.com) they you would just override the first one. If what you want is just a list of emails, adresses and names wouldn't it be better to make a class called person or something and add those to a list?

Like person(name,email,adress) then add that to a list.

Lyrion
  • 426
  • 6
  • 21
1

when storing object in hashmap, you can use the hashcode of that object as key and then can save the object itself as the value, but make sure that you implement hasCode() and equals method properly, as u know, hashmap internally used hashcode() and 'equals` method for storing data.

Now when implementing hashcode or equals method, you can use any attribute (column of that row), which u think, that uniquely identify that row.

And moreover, performance wise this will be a better approach.

M.J.
  • 16,266
  • 28
  • 75
  • 97
1

You can make a map like as HashMap<String, Data> where the first argument is the key (I suppose a String, you can use what you want) and Data is a a class that contains the data values for the key. Data may be:

public class Data {

    private String name;
    private String address;
    private String email;

    ...
}

You can add object to the map with map.put(key, new Data(...)). A more simple way is use array, in a map like HashMap<String,String[]>, but is not very useful. The Java idea with DB query is to create object to encapsulate every record.

Alberto
  • 1,569
  • 1
  • 22
  • 41