Results 1 to 3 of 3
  1. #1
    dogeatdogfood is offline Member
    Join Date
    May 2012
    Rep Power

    Default removing from a hashmap and adding to the end

    hi guys,

    Just come across the forum, and it seems a great place to learn for someone only just starting out in java!

    Wondering if anyone can assist

    I am trying to write a method that represents a rota by removing an entry in a hashmap and adding it to the end, however the code I have at the moment works for removing the entry but when I put it back it goes in its original place:

    Java Code:
        public void cleaningRota(String justCleaned)
            ArrayList<Integer> removedKey = cleanVolunteers.remove(justCleaned);
            cleanVolunteers.put(justCleaned, removedKey);
            System.out.println(removedKey);//ignore - used for testing

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power

    Default Re: removing from a hashmap and adding to the end

    Recommend reading on how a HashMap works under the hood - and this is clearly stated in the API
    This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
    This includes the order of entry. If you need to maintain the order of entry of the keys within a Map, use a LinkedHashMap
    Last edited by doWhile; 05-05-2012 at 01:40 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: removing from a hashmap and adding to the end

    HashMap is unordered, so the concept of 'adding to the end' doesn't apply. Read the API for the Map interface and go through the implementing classes; you'll find there's a Map implementation that has
    predictable iteration order.

    cat's out of the bag!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. removing and adding SWING components
    By Athlon* in forum AWT / Swing
    Replies: 1
    Last Post: 12-06-2011, 06:12 PM
  2. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:13 PM
  3. problem with removing element from HashMap
    By checho in forum New To Java
    Replies: 15
    Last Post: 01-10-2010, 01:10 PM
  4. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 12:48 PM
  5. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 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