Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Generics warnings

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generics warnings

    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)
    and
    name = record.get("Name") // To return the data in the field name

    Any comments would be appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Generics
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: May 19th, 2011, 06:44 PM
  2. Arrays and Generics
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2011, 09:44 PM
  3. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 06:08 PM
  4. program execution with warnings
    By prince joseph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 28th, 2010, 03:53 PM
  5. Java EE warnings while parsing wsdl
    By Idy in forum Web Frameworks
    Replies: 0
    Last Post: January 14th, 2010, 03:29 PM