Thread: Hashmaps Vs Array

Results 1 to 2 of 2
  1. #1 Hashmaps Vs Array 
    Registered Member Harambe_'s Avatar
    Join Date
    Jul 2007
    Posts
    503
    Thanks given
    15
    Thanks received
    22
    Rep Power
    13
    Nevermind
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  2. #2  
    Banned Market Banned Market Banned


    Join Date
    Jan 2011
    Age
    23
    Posts
    3,115
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    I don't mean to sound condescending, but be logical. Maps are used for when you need key -> value mappings; for instance, if you wanted to map a weapon to some sort of poison type.

    Code:
    Map<Integer, PoisonType> map = new HashMap<>();
    
    // Here we map an Integer value of "4151" to the PoisonType "MILD"
    map.put(4151, PoisonType.MILD);
    
    // Now we retrieve the value that we just mapped, this will obviously print "MILD"
    System.out.println(map.get(4151));

    Do you need to map anything in this situation? No. Therefore, in this context a Map should not be used. The best alternative in my opinion would be a HashSet (which ironically is backed by a HashMap) because with a proper hashcode and equals implementation this collection will probably yield the best performance. Set's also do not allow duplicates, which in this case is perfect (you wouldn't want your minigame to have duplicate instances of the same player, would you?).

    Code:
    static Set<Player> players = new HashSet<>();
    
    void join(Player p) {
        if(players.add(p)) {
            p.move(new Position(...));
            p.sendMessage("You enter the minigame!");
            players.forEach(plr -> plr.sendMessage(p.username+"  has just joined the minigame!"));
        }
    }

    Or if you know nothing about hashcode and it's agreement with equals, and how that pertains to the performance of hash based collections then a simple LinkedList will do. Although you'll have to check if the list already contains the player.

    Code:
    static List<Player> players = new LinkedList<>();
    
    void join(Player p) {
        if(players.contains(p))
            return;
        players.add(p);
        p.move(new Position(...));
        p.sendMessage("You enter the minigame!");
        players.forEach(plr -> plr.sendMessage(p.username+"  has just joined the minigame!"));
    }

    You may also be interested in giving [Only registered and activated users can see links. ] a little read if you want to learn a lot more.
    Reply With Quote  
     

  3. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Speed comparison between Array, ArrayList, and HashMap
    By Conscientia in forum Informative Threads
    Replies: 5
    Last Post: 01-08-2014, 11:56 AM
  2. [Java] Enums vs Arrays
    By Shawn Patel in forum Application Development
    Replies: 3
    Last Post: 03-13-2013, 09:25 AM
  3. Array vs enum
    By Andrew in forum Help
    Replies: 6
    Last Post: 09-06-2012, 04:02 AM
  4. Arrays Vs Enums.
    By Gluon in forum Application Development
    Replies: 44
    Last Post: 02-26-2012, 05:18 AM
  5. Replies: 44
    Last Post: 07-03-2009, 12:16 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •