Collection ...

Posted on 2006-04-26
Last Modified: 2010-03-31
Hello experts,

   I have the following file:
  Animal, Type
  turtle, reptile
  lizard, reptile
  snake, reptile
  dog, mammal
  cat, mammal
  rat, mammal
 shark, fish
 goldfish, fish

   I want them in a structure like:
   reptile : turtle, lizard, snake
   mammal: dog, cat, rat
   fish : shark, goldfish

  What type of collection and structure should I use to solve this problem ? thanks.
Question by:meow00
    LVL 19

    Accepted Solution

    I'd probably approach this using a Map of Lists. You'd have a Map keyed by animal genus (or whatever it is) where the value of the map was a List which contained the species. Something like:

        Map genus = new HashMap();
        for each line in the file
            String split = line.split(",");
            List species = genus.get(split[0]);
            if (species == null) {
                species = new ArrayList();
                genus.put(split[0], species);


    LVL 19

    Expert Comment

    by:Jim Cakalic
    Hi, meow00. Did my comment answer your question?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Tips on building some plugins or small utility apps 3 54
    hash value 2 29
    advance java 4 34
    mapShare challenge 13 40
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now