我正在尝试比较哈希映射中数据的键以检查数据是否存在。但是,即使数组中存在键,containsKey仍将返回false。我正在使用这个:
package timeTraveler.core;
import java.util.Arrays;
public class StringArrayHolder
{
private final String[] data;
//I do not want any client could change the array reference
//this also explains why this field doesn't have a setter
public StringArrayHolder(String[] data)
{
this.data = data;
}
public String[] getData()
{
return this.data;
}
@Override
public int hashCode()
{
return Arrays.hashCode(data);
}
@Override
public boolean equals(Object o)
{
if (o == null)
{
System.out.println("NULL");
return false;
}
if (o == this)
{
System.out.println("THIS");
return true;
}
if (o instanceof StringArrayHolder)
{
StringArrayHolder other = (StringArrayHolder)o;
return Arrays.equals(this.data, other.data);
}
return false;
}
//just to print in console for testing purposes
@Override
public String toString()
{
return Arrays.deepToString(data);
}
}
我的比较:
package timeTraveler.mechanics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import timeTraveler.core.StringArrayHolder;
public class PathingData
{
/**
* Entity data array
*/
public Map<StringArrayHolder, List<int[]>> allEntityData;
public PathingData()
{
allEntityData = new HashMap<StringArrayHolder, List<int[]>>();
}
/**
* Adds an entity identifier (NBT)and MobType to the entity data ArrayList. If the entity already exists inside of the ArrayList, then it skips it.
* @param uuid
*/
public void addEntity(StringArrayHolder entityData)
{
System.out.println(entityData);
if(!allEntityData.containsKey(entityData))
{
System.out.println("Adding entity!");
allEntityData.put(entityData, new ArrayList<int[]>());
}
else
{
System.out.println("ENTITY ALREADY EXISTS IN ARRAY");
}
}
/**
* Adds data (X, Y, and Z) to the corresponding identifier (NBT) for the entity. If the entity's identifier does not exist, then it prints out a line that says the identifier cannot be found.
* @param uuid
* @param data
*/
public void addData(StringArrayHolder entityData, String data)
{
System.out.println(entityData);
if(allEntityData.containsKey(entityData))
{
System.out.println("Adding data to entity!");
int[] rawData = new int[3];
String[] pureData = data.split(",");
rawData[0] = Integer.parseInt(pureData[0]);
rawData[1] = Integer.parseInt(pureData[1]);
rawData[2] = Integer.parseInt(pureData[2]);
List<int[]> entityLocData = allEntityData.get(entityData);
entityLocData.add(rawData);
allEntityData.put(entityData, entityLocData);
}
else
{
System.out.println("ENTITY DOES NOT EXIST IN ARRAY! :(");
}
}
/**
* Gets the data for a specific UUID (Unique ID) for an entity.
* @param uuid
* @return
*/
public List<int[]> getDataForIdentifier(StringArrayHolder entityData)
{
List<int[]> entityLoc = allEntityData.get(entityData);
return entityLoc;
}
/**
* Clears all entities and their corresponding data from the map.
*/
public void clearAllEntitiesAndData()
{
allEntityData.clear();
}
/**
* Checks if entity exists inside of array
* @param entityData
* @return
*/
public boolean doesEntityExist(StringArrayHolder entityData)
{
System.out.println(entityData);
if(allEntityData.containsKey(entityData) || allEntityData.containsValue(entityData))
{
System.out.println("ENTITYDOESEXISTINARRAY :)");
return true;
}
System.out.println("ENTITY DOES NOT EXIXT IN ARRAY! :(");
return false;
}
@Override
public String toString()
{
return allEntityData.toString();
}
}
调用doEntityExist的区域:
package timeTraveler.entities;
import java.util.Random;
import java.util.UUID;
import timeTraveler.core.StringArrayHolder;
import timeTraveler.core.TimeTraveler;
import timeTraveler.gui.GuiTimeTravel;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
public class EntityHandler
{
boolean inPast;
@ForgeSubscribe
public void onEntityJoin(EntityJoinWorldEvent event)
{
inPast = GuiTimeTravel.isInPast;
if(event.entity instanceof EntityLiving && !(event.entity instanceof EntityPlayer))
{
NBTTagCompound entityData = event.entity.getEntityData();
Random rand = new Random();
if(entityData.getInteger("identifier") == 0)
{
entityData.setInteger("identifier", rand.nextInt());
}
String[] data = new String[2];
String identifier = Integer.toString(event.entity.getEntityData().getInteger("identifier"));
String entityName = event.entity.getEntityName();
data[0] = identifier;
data[1] = entityName;
StringArrayHolder d = new StringArrayHolder(data);
if(inPast)
{
System.out.println(data[0]);
System.out.println(data[1]);
System.out.println(d);
if(!TimeTraveler.vars.pathData.doesEntityExist(d))
{
System.out.println("REMOVING ENTITY");
//event.entity.setDead();
}
else
{
System.out.println(":)");
}
}
}
}
}
具体来说,doEntityExist总是返回false。数据确实存在于数组中,但由于某种原因它仍然返回false。有什么想法吗?