如何每秒向哈希图中添加一个整数,然后检查它何时达到预期的最终数字?
How do I add an integer to a hashmap every second, then check when it reaches the intendid final number?
我正在练习学习 java,我想学习的东西比我自己能找到的要多得多,所以我问这个问题。我知道如何创建哈希图,我知道如何将东西添加到哈希图中,我想知道如何将一个名为 "Timmy" 的对象添加到哈希图的一侧,然后添加一个每秒增长的整数1 进入另一侧。一旦它达到让我们说 400 它会做一些像添加 "Timmy" 到另一个哈希图中,然后到另一个哈希图中......如果你明白我在说什么。我还想知道一旦他们加入了下一层 hashmap 如何做某事。
这是我的意思的一个例子:
Timmy 加入第一个 hashmap > 等待 400 秒 > 被提升到下一个 hashmap > Console.log("You have reached the second hashmap!") > 这次等待 800 秒 > 被提升到第三个 hashmap > Console.log("You Win, by advancing through all the hashmaps.").
我会在所有尝试都失败后回来,很抱歉我这样提出问题。我知道如果我不完全评估我的问题试图得到答案是不礼貌的。
您可以执行以下操作:
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++;
}
希望对您有所帮助!!! :D
我正在练习学习 java,我想学习的东西比我自己能找到的要多得多,所以我问这个问题。我知道如何创建哈希图,我知道如何将东西添加到哈希图中,我想知道如何将一个名为 "Timmy" 的对象添加到哈希图的一侧,然后添加一个每秒增长的整数1 进入另一侧。一旦它达到让我们说 400 它会做一些像添加 "Timmy" 到另一个哈希图中,然后到另一个哈希图中......如果你明白我在说什么。我还想知道一旦他们加入了下一层 hashmap 如何做某事。
这是我的意思的一个例子:
Timmy 加入第一个 hashmap > 等待 400 秒 > 被提升到下一个 hashmap > Console.log("You have reached the second hashmap!") > 这次等待 800 秒 > 被提升到第三个 hashmap > Console.log("You Win, by advancing through all the hashmaps.").
我会在所有尝试都失败后回来,很抱歉我这样提出问题。我知道如果我不完全评估我的问题试图得到答案是不礼貌的。
您可以执行以下操作:
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++;
}
希望对您有所帮助!!! :D