java - 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);
解决方案
与其调用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;
推荐阅读
- angular - 自定义错误处理程序未捕获 zone.js 中的异常
- python - 你怎么称呼这种类型的类声明?
- google-bigquery - 从数据洞察访问 BigQuery 行级过滤表
- antlr4 - Antlr 任何字符直到字符串
- amazon-web-services - 如何设计一个事件监听器应用程序只处理一次事件,并且是 HA 和可扩展的?
- java - Spring JPA 抽象/继承关系
- python-3.9 - Pycryptodome Diffie Hellman 密钥交换
- sql - 为什么我的游标没有返回第一行值?
- laravel - Laravel Passport API 总是从前端返回 401 未授权
- assembly - 获取带有地址的控制流图,如程序集中所示