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

Thread: Should i use a hashmap?

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

    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

    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
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Should i use a hashmap?

    HashMaps maps one key to one item. You can map the same item to multiple keys. However, in a HashMap all keys must be unique so you can't map multiple items to the same key. I don't see HashMaps helping you anymore than ArrayLists (on the contrary, they would be more cumbersome).

    If you're allowed to change the the artist (or event) class, that would be the way to go. Simply add to the artist (or event) class any collection you like of events. By virtue, any event could turn up in different artist's collections of events, and an artist can contain multiple events.

    Otherwise, you could look into using graphs. Simply define a graph which can contain both events and artists as nodes, then define edges which would link artists to their associated events and vise-versa. Technically such a graph layout could allow you to associate an artist with another artist, or an event with another event, but if you properly encapsulate your graph (and/or use it smartly) you can avoid this.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    6Sloth9 (May 1st, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Should i use a hashmap?

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/43354-should-i-use-hashmap.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM
  2. reading file into a hashmap?
    By minotaurus in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 4th, 2011, 04:35 AM
  3. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  4. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 11:56 AM
  5. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM

Tags for this Thread