I programmed in Java about 12 years ago. Since then I have been using python. It was a real nuisance changing existing code from from 2.6,2.7 to 3 and it prompted me to take another look at java.
One application I use (I write code for my own use) reads a text file where each line is lice a database record split into fields. In python I have a class that will read the file, create a dict of each record (using field names to retrieve the string data) and store the records in a list.
My first thought was to replicate this in java. The records could be a HashMap and the list of records an ArrayList (the number and names of fields is unknown at compile time as is the number of records.
There may be better ways to do this in java.
When I code :
ArrayList records = new ArrayList();
the editor (eclipse) provides a warning about being parameterized. So then I tried
ArrayList<HashMap> records = new ArrayList<HashMap>();
And now the editor complains that HashMap needs to be parameterized - so I try
ArrayList<HashMap<String, String>> records = new ArrayList<HashMap<String, String>>();
This seems a bit excessive and made me think I was using the wrong approach.
The class that reads the file needs to have an interface that provides
rec = txtFile.getRecord(int i) // returning a record (a HashMap in my implementation)
name = record.get("Name") // To return the data in the field name
Any comments would be appreciated.
Re: Generics warnings
You have the right approach if you want type safety. Generics aren't required of course (the compiler will just warn you), but they provide type checking at compile time, which avoids the ugliness and error prone runtime casting when you retrieve values from the List or Map.