Results 1 to 2 of 2
  1. #1
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default HashMap String Return

    Hi,

    I'am having difficulty to make this work.

    I have a class named Contest and List. Contest constructor has few parameters and also have another parameter which is called 'locations'. What i need to do is that in my List class, I have to use the return method i have created in Contest class and use that method to return all locations used for contests in the HashMap that I created in List class. It is just too difficulty, confusing :confused: I can't even demonstrate the idea clearly lol Help please :? :(

    regards

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class ContestLocations {
        public static void main(String[] args) {
            ContestList contestList = new ContestList();
            // Add some Contests.
            contestList.addContest(new Contest("sing", "12 Mar", "Dallas, TX"));
            contestList.addContest(new Contest("draw", "19 Mar", "Tampa, FL"));
            contestList.addContest(new Contest("ride", "22 Mar", "Stockton, CA"));
            contestList.addContest(new Contest("sail", "22 Mar", "Tampa, FL"));
            contestList.addContest(new Contest("ride", "29 Mar", "Cheyenne, WY"));
            contestList.addContest(new Contest("sail", "30 Mar", "Santa Cruz, CA"));
            // Get the locations of the contests.
            List<String> locs = contestList.getLocations();
            System.out.println("locs = " + locs);
        }
    }
    
    class Contest {
        String name;
        String date;
        String location;
    
        Contest(String name, String date, String location) {
            this.name = name;
            this.date = date;
            this.location = location;
        }
    
        public String getLocation() { return location; }
    }
    
    class ContestList {
        List<Contest> list = new ArrayList<Contest>();
    
        public void addContest(Contest contest) {
            list.add(contest);
        }
    
        public List<String> getLocations() {
            List<String> allLocations = new ArrayList<String>();
            for(int j = 0; j < list.size(); j++) {
                Contest contest = (Contest)list.get(j);
                String location = contest.getLocation();
                // Avoid duplicate locations.
                if(!allLocations.contains(location)) {
                    allLocations.add(location);
                }
            }
            return allLocations;
        }
    }

Similar Threads

  1. Soft HashMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:45 PM
  2. HashMap and ComboBox
    By banie in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2008, 11:58 PM
  3. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM
  4. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 01:23 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •