Solved HashMaps..

Discussion in 'Plugin Development' started by ItsOneAndTwo, Feb 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ItsOneAndTwo

    I've never used hashmaps before, so, what i currently have, is this:
    Code:java
    1. public HashMap<Long, Block> someStuff = new HashMap<Long, Block>();


    I use this method to store stuff into the HashMap:
    Code:java
    1. plugin.someStuff.put(System.currentTimeMillis(), c.getBlock());


    Now I need to check the HashMap for an expired entry (In that case, check if the currentTimeMillis is older than 10 minutes), and if so, get the block in the expired entry, and do stuff with it.

    How would I do that?
     
  2. Offline

    diage

    Personally, I would create a container class that has the value of the time and the block your interested in as well as the associated entries and then use a Queue instead of a map. With a map you will need to search through it every time to get the items you want, however with a queue you can assume First in First out order (not a luxury you get from maps).

    To do this -

    Create a class with a constructor that takes in your time and a block, create a getter method for each and fields for each.

    Then create a queue such as:
    Queue<MyTimeBlockHolderClass> timedBlocks = new Deque<>()

    You can add elements with the add(e) method and remove whatever is chronologically next with the remove command. You can also call peek to see what is at the end of the queue without removing it.


    As a style note, don't make fields public.

    Reference the java 7 API for more on queues, they're under java.util
     
  3. Offline

    Traks

    I think this would work, haven't tested (and might contain errors)
    Code:java
    1. private HashMap<Long, Block> someStuff = new HashMap<Long, Block>();
    2.  
    3. public void addBlock(Block block) {
    4. if(block != null)
    5. someStuff.put(TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis()), block);
    6. }
    7.  
    8. public void doStuff() {
    9. long current = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis());
    10.  
    11. for(Entry<Long, Block> entry : someStuff.entrySet()) {
    12. if(entry.getKey() + 10 <= current) {
    13. // Do stuff with entry.getValue()
    14. }
    15. }
    16. }
     
    ItsOneAndTwo likes this.
  4. Offline

    ItsOneAndTwo

    diage
    Could you give me any code?


    Gonna try that :3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    diage

    I could.. but what would the educational value in that be? Technically you could just for loop through it, but what an unsophisticated answer that is :)

    But, at the very least, here is a hint - This is the container class you might want to use.

    Code:java
    1. public Class MyTimeBlockClass {
    2. private long time;
    3. private Block block;
    4.  
    5. public MyTimeBlockClass(long time, Block block) {
    6. this.time = time;
    7. this.block = block;
    8. }
    9.  
    10. public long getTime() {
    11. return this.time;
    12. }
    13.  
    14. public Block getBlock() {
    15. return this.block;
    16. }
    17. }
     
    The Fancy Whale likes this.
  6. Offline

    ItsOneAndTwo

    Traks
    I got everything working as it should!
    Thank you very much!
     
  7. Offline

    Traks

    diage Why use a class if you only need to store 2 variables and can thus use a hashmap?
    ItsOneAndTwo Btw, might wanna set the key to the block and the value to the minutes, so you can store multiple blocks per minute; when adding another block in the same minute it will overwrite the previous one in the code I showed
     
  8. Offline

    ItsOneAndTwo

  9. Offline

    diage

    Well, that is called a container class, if you are using the considerably ore efficient Queue, you can guarantee the order in which the items are in the collection and no longer need to use a for loop which will have a run time equivalent to the size of the map.

    The point of creating it is to establish a strong sense of object orientation and allows you to pass it into the queue as an object. When coding these things, this might be a small plugin - sure, but it is generally beneficial to always take the most efficient route when that options pops up. Not to mention, jumping out of your comfort zone in coding can open you up to learn much more.

    In short, you will find the solution i provided to be a bit more complicated but potentially (depending on the size of the map) considerably more efficient (constant run time versus linear)

    BTW.. you want to make sure that the .equals method is correctly implemented before setting anything as the key to a map. I think block fully implements .equals, but it would be worth verifying.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    Traks

  11. Offline

    diage

    It's just a good habit to get into to verify that, if you go on assuming it is implemented then you will get a map with highly unpredictable behavior when you run into a case when it isn't implemented how you think it should be. That is mainly for ItsOneAndTwo's benefit.
     
  12. Offline

    Traks

    diage I always check...
     
  13. Offline

    diage

    Like I said, more for his benefit :)
     
Thread Status:
Not open for further replies.

Share This Page