I am learning Java and have a pretty basic problem.
I am indexing some sites with a BufferedReader and storing the data in a MySQL-base. I do this for 30 sources every 15 seconds, which generate a lot of data.
Now I want to analyze this data. I am thinking of storing the data simultaneously in a HashMap which I will clear at the end of the day, every day.
But can you give me an example on how to create an object for 30 different sources?
Do I need 30 different HashMap or can I build a key like ‘pathName+randomNumber’?
In the end I want to be able to locate the first entry in the HashMap for each source and the two latest entries thereby enabling me to see what the difference between these three are.
Please help. I have tried to look on the web but with no luck as I think the HashMap examples are always focused towards storing Objects, but not on how to create the objects you store in them… (yeah I know – it’s a rookie question) 😉
Jobu, I would do exactly as you proposed. So in a sense you answered your question already. 🙂 Whenever I had a similar situation, and had to use a Map, I use forward slashes to form a hierarchy of objects. So, say you want to distinguish data that come from 30 sources as you say. – Give those sources a unique name, and form your Map keys using this simple pattern: “
/<root>/<source id>/<data id>” . Example of storing a link on some website:mymap.put("/myproject/www.example.com/link-0121", "http://www.kernel.org");If, for some reason you want to get all Map entries that have keys equal to “
/myproject/www.example.com“, then I suggest you List objects only to your map! Here is a pseudo-code for that case:This approach works only if your data can be uniquely identified by a number. Then you simply use List’s get() method which has index as parameter.
However, if you need to have string keys for data, I am afraid you need to make Map of Maps (ie. use HashMap instead of the ArrayList).