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 6 of 6

Thread: Confused on how to add data from a list into a HashMap

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confused on how to add data from a list into a HashMap

    I am being asked to do the following:
    where the keys are names of runners and the values are their times. The keys should be sorted at all times.

    This method should iterate over*runners, populating the maps*juniorResults,standardResults*and*seniorResul ts, with the correct names and times of runners.

    runners is an ArrayList that stores the information.

    I have managed to get working:

    for (Runner runner: runners)
    if(runners.contains("Junior")

    I am confused on how to get the names of the runners and the times from the ArrayList so I can populate each of the maps with the right key/value. I am using BlueJ.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Confused on how to add data from a list into a HashMap

    Can you paste the code of your Runner class?

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on how to add data from a list into a HashMap

    Of course. I just realized I am missing an import for the class but otherwise that's the whole Runner class.

    /**
     * Class Runner - Simulates a runner in a Marathon
     * 
     *
     */
    public class Runner implements Comparable<Runner>
    {
    /* static variables */
     
    // To be added by students in Question 3, part (i)(a) and part(iv)(a)
    private static int nextNumber = 1; //intializes nextNumber and sets it to 1
    /* instance variables */
     
       private int number;       // runner's number
       private String name;      // runner's name
       private String ageGroup;  // standard, junior or senior
       private int time;         // runner's marathon time in minutes
     
     
       /**
        * Default constructor for objects of class Runner
        */
       public Runner()
       {
          super();
          this.name = "";
          this.ageGroup = "standard";
          this.time = 0;
     
     
     
        // additional code to be added by students in Q3, part (i)(b)
         this.number = nextNumber; //sets the value of number to 1
        this.number = nextNumber++; //increases the total of number by the value of nextNumber 
     
     
     
     
     
     
     
       }
     
        /**
         * 
         */
        public int compareTo(Runner anotherRunner)
        {
          return this.getTime()-(anotherRunner.getTime()); // compares one Runner's time to anotherRunner's time
           }
     
    /* instance methods */
     
        //Only those accessor methods that you will need have been included
     
       /**
        * Returns the receiver's number
        */
       public int getNumber()
       {
          return this.number;
       }
     
     
       /**
        * Sets the receiver's name
        */
       public void setName(String name)
       {
          this.name = name;
       }
     
     
       /**
        * Returns the receiver's name
        */
       public String getName()
       {
          return this.name;
       }
     
     
       /**
        * Sets the receiver's ageGroup
        */
       public void setAgeGroup(String group)
       {
          this.ageGroup = group;
       }
     
       /**
        * Returns the receiver's ageGroup
        */
       public String getAgeGroup()
       {
          return this.ageGroup;
       }
     
     
       /**
        * Sets the receiver's time
        */
       public void setTime(int time)
       {
          this.time = time;
       }
     
     
       /**
        * Returns the receiver's time
        */
       public int getTime()
       {
          return this.time;
       }
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confused on how to add data from a list into a HashMap

    how to get the names of the runners and the times
    Are those values contained in the Runner objects? Does the Runner class have accessor methods for getting them?
    If the ArrayList contains Runner objects, get the objects from the array list and use the accessor methods to get the contents you are looking for.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on how to add data from a list into a HashMap

    I believe the values are but the Maps will be created in another class MarathonAdmin and I have tried to use getTime() and getName(), but when I try using it to populate the Map, BlueJ says the methods are not known or declared. Unless I need to create a new Runner object within the method and use the accessors that way.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Confused on how to add data from a list into a HashMap

    says the methods are not known or declare
    Please copy the full text of the error messages and paste it here.
    Also post the code where the errors happen.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Want to Add Duplicate Key in HashMap
    By rampnq in forum Collections and Generics
    Replies: 8
    Last Post: January 17th, 2018, 05:01 PM
  2. Getting confused need to add a for loop
    By iamgonge in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 25th, 2013, 04:40 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. Replies: 1
    Last Post: April 26th, 2011, 08:47 AM
  5. Spreadsheet data - linked list or hashmap
    By j919 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2011, 06:04 PM