Search code examples
javahashmaptimed

How do I add an integer to a hashmap every second, then check when it reaches the intendid final number?


I am practicing learning java and I would like to learn a lot more than I can find on my own hence I am asking this question. I know how to create a hashmap, I know how to add things into the hashmap, I would like to know how I add an object called "Timmy" into one side of the hashmap and then add an integer that grows every second by 1 into the other side. Once it has reached lets say 400 it will do something like add "Timmy" into another hashmap, and then into another hashmap... If you understand what I am saying. I would also like to know how to do something once they have joined the next tier hashmap.

Here is an example of what I mean:

Timmy joins 1st hashmap > Waits 400 seconds > Gets promoted to next hashmap > Console.log("You have reached the second hashmap!") > Waits 800 seconds this time> Gets promoted into the third hashmap > Console.log("You Win, by advancing through all the hashmaps.").

I will come back when all attempts fail, I am sorry that I presented my question like this. I understand that it was rude of me not to fully evaluate what my question is trying to get an answer for.


Solution

  • You could do something like the following:

    int total = 100;
    int i = 0;
    
    // You should use a more specifc type than Object if you can
    Map<Integer, Object> m = new HashMap<Integer, Object>();
    
    while (i < total) {
    
        Object o = //what you want to put into the HashMap on each iteration
    
        m.put(i, o);
    
        Thread.sleep(1000);
        i++;
    }
    

    Hope this helps!!! :D