• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database to Hashmap

 
Mauro Mazzucco
Ranch Hand
Posts: 32
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say i have a database with 2 tables:
Car
Model

in Car i have the columns:
carID........carName:........modelID:
0001...........Toyota..............123
0002..............GM ...............456

in Model i have:
modelID..........modelName.............. modelYear
123................... Lexus...................... 2002
456....................Passat......................2005

I want to store the modelID as a key and the values will be a List with the other columns of the corresponding modelID, something like
Key=123 Values= 0001, Toyota, Lexus, 2002
Key=456 Values= 0002, GM, Passat, 2005




 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Mauro,

You can do that , it is possible. Just an idea, you can have a HashMap of keys as ModelID ( integer ) and values as a bean containing the corresponding values. Something like a Map<Integer, Bean> . Here the bean can contain your values.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your question is (what have you tried so far?), but I can see a couple of problems with that design.

Firstly, having a list where the first element is the ID, the second is the name, etc, isn't a good idea. A much better idea is to create a class with fields containing the values you want, and then have a map of ID to object.

Secondly, that doesn't seem to match your data design. The way you've described it suggests that for a given model ID there's going to be a single car ID and car name. Is that correct? If so your tables are badly designed. The table design (assuming carID is a primary key of the Car table, and modelID is a primary key of the Model table, which would make sense) suggests that you can have more than one Car for each Model. Which is the right interpretation?

Maybe you can describe what the data is supposed to represent and we can advise you further.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can do that but why? Why do you need to drag those data out of the database when you can retrieve them easily as required?
I would suggest the key would be a LONGINT in SQL, so you would use a Long as the “K” in a Map, and a Car object as the “V”.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mauro Mazzucco wrote:I want to store the modelID as a key and the values will be a List with the other columns of the corresponding modelID, something like
Key=123 Values= 0001, Toyota, Lexus, 2002
Key=456 Values= 0002, GM, Passat, 2005

My advice: DON'T.

Create a class for each table that looks like a single row, and then put instances of those into a Map.

Winston
 
Mauro Mazzucco
Ranch Hand
Posts: 32
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'm not sure what your question is (what have you tried so far?), but I can see a couple of problems with that design.

Firstly, having a list where the first element is the ID, the second is the name, etc, isn't a good idea. A much better idea is to create a class with fields containing the values you want, and then have a map of ID to object.

Secondly, that doesn't seem to match your data design. The way you've described it suggests that for a given model ID there's going to be a single car ID and car name. Is that correct? If so your tables are badly designed. The table design (assuming carID is a primary key of the Car table, and modelID is a primary key of the Model table, which would make sense) suggests that you can have more than one Car for each Model. Which is the right interpretation?

Maybe you can describe what the data is supposed to represent and we can advise you further.

Well i tried to simplify the problem but i think i made it worst, let me give you the full thing:

Your program must do:
1 - Read a carinfo txt file with the format
Data: MMddyyyy
ModelID: mmmmmm (Always 6 numbers)
CityID: ccccc (Always 5 numers)
carID: xxxxxxxxx (no fixed size)
eg.
mmDDyyyymmmmmmcccccxxxxxxxxxxxxxxxx
e.g
1005201200012210020565765765453543543
2 – Read a model CSV file
modelID
modelName
modelYear
brand
eg:
234211;A1;2013;AUDI
3 – Read a city file in CSV format
CityID
cityName
state
country
eg:
10220;NYC;NY;USA
4 – Store all info into a database.
5 – Create a window with 4 buttons:
a) By Date
b) By Model
c) By City
d) Details
each window shows the carinfo data grouped by whats on the button, each window must have a table that show all possible information
e.g.
ModelID: mmmmm
ModelName: A1
Model Year: 2013
Brand: Audi
Date: 01/01/1970
carID:xxxxxxxxxx
ModelID: mmmmm
ModelName: Lexus
Model Year: 2001
Brand: GM
Date 01/01/1999
carID:xxxxxxxxxx

So i did 3 tables, one for each file, and now i need to retrieve the data and show like that, so i thought i could use a HashMap to put all values related to a modelID in the Map and then display into a JTable
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as possible, do all that sorting in the SQL. Database programs maintain indices which can sort millions of data in seconds, and they are specially optimised for that sort of work. This tutorial may help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic