我有一个HashMap <UUID,ArrayList<util.Package>>
类型的哈希图。哈希表工作正常,但不存储ArrayList。我可以向其中添加项目,但是它们不能在那里保留。
util.Package是一个枚举类型,其中包含两个枚举。
我尝试了不同的添加方式(直接通过地图:hashMapName.get(uuid).add(util.Package.TYPE)
,并通过获取对象,将其从地图中删除,添加到地图中,然后再放回去。)均未奏效。
加入事件:
loadAndCoins.newPlayer(e.getPlayer().getUniqueId());
loadAndCoins.newPlayer():
public static boolean newPlayer(UUID uuid) {
if (!balances.containsKey(uuid)) {
balances.put(uuid, 0);
experience.put(uuid,
GameStats.createNewPlayer(uuid);
Packages.newPlayer(uuid); // this is the line that creates
return true;
}
return false;
}
Packages.newPlayer():
public static void newPlayer(UUID uuid) {
ArrayList<util.Package> n = new ArrayList<util.Package>();
n.add(util.Package.JMSG_DEFAULT);
System.out.println("NEW: " + n); // debug, has JMSG_DEFAULT
Packs.put(uuid, n);
}
Packages.addPackage():(将软件包添加到uuid)
public static boolean addPackage(UUID uuid,util.Package pack) {
if (!Packs.containsKey(uuid)) return false;
if (!Packs.get(uuid).contains(pack)) {
ArrayList<util.Package> op = Packs.get(uuid);
op.add(pack);
Packs.remove(uuid);
Packs.put(uuid, op);
System.out.println("ADDPACKAGE: " + op);
// has DEFAULT and the added package
return true;
} return false;
}
Packages.hasPackage():(用于检查uuid是否包含软件包)
public static boolean hasPackage(UUID uuid,util.Package pack) {
if (!Packs.containsKey(uuid)) return false;
System.out.println(Packs.toString()); //has only DEFAULT.
if(Packs.get(uuid).contains(pack)) return true;
return false;
}
预期: 首先调用addPackage(uuid,Package.JMSG_FESTIVE), 然后调用hasPackage(uuid,Package.JMSG_FESTIVE) 将返回true。 实际:返回false。调试打印的返回内容被注释,因此很容易理解。
util.Package的内容:
package util;
public enum Package {
LUCK_1,LUCK_2,LUCK_3,LUCK_4,LUCK_5,SPEED_1,SPEED_2,SPEED_3,SPEED_4,SPEED_5,J
MSG_DEFAULT, JMSG_COMPUTERSPEAK, JMSG_RAINBOW, JMSG_ROYALTY,
JMSG_FESTIVE,SHOVEL_STONE,SHOVEL_IRON,SHOVEL_GOLD,
}
答案 0 :(得分:1)
这对我来说很好:
public final class Foo {
public static void main(String[] args) { //Just for testing
final UUID uuid = UUID.randomUUID();
newPlayer(uuid);
addPackage(uuid, Package.LUCK_1);
addPackage(uuid, Package.LUCK_2);
System.out.println(hasPackage(uuid, Package.LUCK_1));
System.out.println(hasPackage(uuid, Package.LUCK_2));
System.out.println(hasPackage(uuid, Package.LUCK_3));
System.out.println(PACKAGE_MAP.toString());
}
private static final Map<UUID, List<Package>> PACKAGE_MAP = new HashMap<>();
public static void newPlayer(UUID uuid) {
List<Package> n = new ArrayList<>();
n.add(Package.JMSG_DEFAULT);
PACKAGE_MAP.put(uuid, n);
}
public static boolean addPackage(UUID uuid, Package pack) {
if (!PACKAGE_MAP.containsKey(uuid)) return false;
if (!PACKAGE_MAP.get(uuid).contains(pack)) {
List<Package> op = PACKAGE_MAP.get(uuid);
op.add(pack);
//removed `remove` and `put` here.
return true;
}
return false;
}
public static boolean hasPackage(UUID uuid, Package pack) {
if (!PACKAGE_MAP.containsKey(uuid)) return false;
return PACKAGE_MAP.get(uuid).contains(pack);
}
}
包装等级
public enum Package {
LUCK_1,
LUCK_2,
LUCK_3,
LUCK_4,
LUCK_5,
SPEED_1,
SPEED_2,
SPEED_3,
SPEED_4,
SPEED_5,
JMSG_DEFAULT,
JMSG_COMPUTERSPEAK,
JMSG_RAINBOW,
JMSG_ROYALTY,
JMSG_FESTIVE,
SHOVEL_STONE,
SHOVEL_IRON,
SHOVEL_GOLD
}
输出:
true
true
false
{55fc709a-7a25-45e6-9ff2-b48345747d39 = [JMSG_DEFAULT,LUCK_1,LUCK_2]}
一些注意事项:如果您在引用{{1的同时修改List
,则无需删除Map
到List
}}放在List
内。