首页 > 解决方案 > Hashmap 不返回整数?

问题描述

我有两个哈希图:

private HashMap<UUID, Integer> codeMap = new HashMap<UUID, Integer>();
private HashMap<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();

我已将以下数据添加到哈希图中:

Integer randomcode = new Random().nextInt(800000)+200000;
p.sendMessage("codice:" + randomcode);
codeMap.put(p.getUniqueId(), randomcode);
uuidMap.put(randomcode, p.getUniqueId());
return true;

下面这段代码代表玩家退出事件:即使在玩家退出之后,该代码仍然存在于 hashmap 中

Integer code = this.codeMap.get(e.getPlayer().getUniqueId());
UUID uuid = this.uuidMap.get(code);
this.codeMap.remove(uuid);
this.uuidMap.remove(code);

标签: javahashmapbukkit

解决方案


与其调用p.getUniqueId()两次,不如调用一次,如下所示:

Integer randomcode = new Random().nextInt(800000)+200000;

// Store the value returned by p.getUniqueId() to a variable
UUID uuid = p.getUniqueId();

p.sendMessage("codice:" + randomcode);
codeMap.put(uuid, randomcode);
uuidMap.put(randomcode, uuid);
return true;

推荐阅读