Results 1 to 8 of 8
  1. #1
    6Sloth9 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Should i use a hashmap?

    Hey guys. I heard good things of this forums so i came to give it a try. Please be patient on me as i am new to java programming or programming in general for that matter.

    So, i'm going to be honest with you. I have an assignment due next week where i need to create a project in java where i would have a class called events, one named artists and another named Simulator where everything takes place. So one artist may be associated with one or more events and one event must have at least one artist. I already tried it with arraylists and got it to work but it is a mess and i did a bit of cheating. Sssshhh don't tell anyone So i thought about associative arrays aka hashtables and/or hashmaps. I know the syntax of these guys, what i lack is the how to implement this. So I thought I should have something like

    Java Code:
    HashMap<events, artists> listOfEvents = new HashMap<events, artists>();
    ArrayList<artists> listOfArtists = new ArrayList<artists>();

    Does this make sense? I know a hashmap consists of a pair (key,value)... I'm lost here guys. Help me.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    So each event can have x artists playing where x is an unknown? If so, it may be helpful to have something more like
    Java Code:
    HashMap<Events, List<Artist>> eventList = new HashMap<Events, List<Artist>>();
    From there you can fill each event with a list of possible artists. Would you mind clarifying what you are having trouble understanding when it comes to hashmaps?


    Also, check out the tutorials for hash maps here: Map Interface | HashMap API

    Your approach should work, do you have any more specific questions? If you are allowed, the actual assignment text may be helpful as well.

    Consider using proper naming conventions as well, classes should have the first letter of each word capitalized. artist should be Artist, event should be Event. Notice I dropped the s off, I am assuming the artist class is representing one individual artist, same with event.
    Last edited by sunde887; 05-01-2011 at 11:38 PM.

  3. #3
    6Sloth9 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks man. That's really helful. I didn't know i could store a list inside a hashmap. Thank you as well for the advice on using proper naming conventions. So, the assignment is to create some kind of tourist guide where people could see a list of events spanning several cultural areas and featuring a set artists. That's the basic mechanics of the project. There are several other features that we could add to the project for valorization purposes. I could provide you with the text for the assignment but it's two pages and i would have to translate it since I live in a non English speaking country.

    So I have the following classes : Simulator (where everything takes place), Event, Artist and CulturalArea. I forgot to tell you that an event is supposed to be of a certain CulturalArea. How can I make that work with the hashmap?

    Thanks a lot once again.
    Last edited by 6Sloth9; 05-01-2011 at 11:57 PM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    So for CulturalArea X it could have y events? Will each cultural area contain a list of events?

    Are you sure you can't just use composition(aggregation) to compose the classes? For example: Each cultural area maintains a list of events, and each event maintains a list of artists.

  5. #5
    6Sloth9 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Sorry you lost me. I'm not familiar with composition/aggregation. If it's easier bring it on :)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, composition is a way of "composing" a class where it maintains another class. It is the favored oo approach, as opposed to inheritance. If you have heard of is-a/has-a relationships we are talking about inheritance and composition.

    you use inheritance when class X is a type of class Y. If this test can be met you can use inheritance. For example, a dog is an animal, so inheritance seems natural
    Java Code:
    class Animal{}
    class Dog extends Animal{}//inheritance
    Composition is the has-a relationship. Lets say for some reason you created a teeth class. Does this relationship make sense to you? A tooth is a dog/ A tooth is an animal? I'd hope not. Instead an animal has-a tooth. Composition looks like this
    Java Code:
    class Animal{}
    class Tooth{}
    class Dog{
      Tooth[] teeth;
    }
    Here Tooth is used as an instance variable of the class dog, which makes sense because a dog has teeth. Obviously, the whole tooth/dog example isn't a very functional example, but I hope it helps explain the difference between inheritance and composition.

    Now, you could do two things, you could have a class called CulturalArea which manages a HashMap<String, HashMap<Event, List<Artist>>>, which would be a string which represents a place in the cultural area, which has an event, which has a list of artists. I'm making the assumption here that each cultural area can have multiple Events. This approach works, but it can be messy. You will have to extract the hashmap at the key of a given string, then extract the list of artists from the returned hashmap, finally you get the list of artists. I don't know about you, but this seems like an annoying amount of work.

    With the power of composition, you can simply make a cultural area have a list of events. The cultural area can do things like add events, remove events, print events, etc. Then each event will manage a list of artists, and the event can do things to the list of artists that it wants.

    Java Code:
    class CulturalArea{
      ArrayList<Event> events = new ArrayList<Event>();
    }
    class Event{
      ArrayList<Artist> artists = new ArrayList<Artist>();
    }
    class Artist{}
    To me, this method is by far easier. Each class provides methods to manipulate the list of the other classes. If you wanted to print a cultural area it would be as simple as defining a toString method which has a little description of the cultural area, which calls the toString method of events, which describes itself, and calls the artist toString method.

    Java Code:
    class Person{
      String name;
      Address a; //a person has-an address(composition)
      public String toString(){
        return "The persons name is: " + name + "\n"
                  + a.toString();
      }
    }
    
    class Address{
      String streetName;
      String city;
      String zip;
      public String toString(){
        return "Address: " + streetName + "\t" + city + "\t" + zip;
      }
    }
    If you are fuzzy on anything ask me more questions and I will help you out the best I can, and I hope this made sense to you.

  7. #7
    6Sloth9 is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Please take a look at these methods and tell me what you think. This is what i had before coming here.


    Java Code:
    Class ControlPanel{
    
    HashMap<Event, Artist> listEvents = new HashMap<Event, Artist>();
    
    ArrayList<Artist> arrayArtists = new ArrayList<Artist>();
    
    public void createEvent()
        {
            String name;
            String nameArtist;
            
            System.out.print("Insert the name: ");
            name = scan.nextLine();
            System.out.print("\fInsert the artist associated with this event: \n");
            viewArtists();
            nameArtist = scan.nextLine();
    
            listEvents.put(new event(name), new artist(nameArtist));
    
         }
    
    public void createArtist()
        {
           String nameArtist;
           System.out.print("\f Insert the name: ");
           nameArtist = scan.nextLine();
           arrayArtistas.add(new Artist(nameArtist));
        }
        
    public void viewArtists()
    {
        Iterator viewArtists = arrayArtist.iterator();
        Artist artists;   
        while (verArtistas.hasNext())
        {
            artists = (Artist) viewArtists.next();
            System.out.println("Name: " + artists.getNameArtist());
        }
    }
    }
    In the Event and Artist classes i have methods like:

    Java Code:
     public String getNameArtist(){
            return nameArtist;
        }

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2011, 12:12 AM
  2. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  3. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 03:29 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
  •