我正在尝试比较两个列表,并将已添加的条目放入一个列表中,并将已删除的条目放入另一个列表中。这是当前的代码:
Map<Chunk, List<Player>> chunkListMap = new HashMap<>();
for (Chunk c : affectedChunks) {
List<Player> currentPlayers = playersInChunk(c);
if (!chunkListMap.containsKey(c)) {
chunkListMap.put(c, currentPlayers);
continue;
}
List<Player> previousPlayers = chunkListMap.get(c);
List<Player> enteringPlayers = new ArrayList<>(currentPlayers);
enteringPlayers.removeAll(previousPlayers);
for (Player player : enteringPlayers) applyEffects(player);
List<Player> leavingPlayers = new ArrayList<>(previousPlayers);
leavingPlayers.removeAll(currentPlayers);
for (Player player : leavingPlayers) unapplyEffects(player);
}
private List<Player> playersInChunk(Chunk c) {
List<Player> retVal = new ArrayList<>();
for (Entity e : c.getEntities()) if (e instanceof Player) retVal.add((Player) e);
return retVal;
}
两个结果列表(enteredPlayers和leavingPlayers)始终为空。我做错了什么?
答案 0 :(得分:0)
虽然不知道业务逻辑或看到完整的代码,但说出原因并不容易。但让我想一想可能的原因:
当一个播放器已经存在于chunkListMap中时,你只会执行for循环之后的代码。那个块c就是你用来检索previousPlayers的东西。
List<Player> previousPlayers = chunkListMap.get(c);
现在您还使用
提取currentPlayers List<Player> currentPlayers = playersInChunk(c);
如果特定Chunk对象中的所有currentPlayers都是Player类型,这将意味着currentPlayers和previousPlayers相同。
然后,如果你这样做:
enteringPlayers.removeAll(previousPlayers);
它会从中删除所有条目。因此,您将看到null。
以下肮脏粗略的例子将告诉您:
package collectionuse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Entity{
String name;
public void setName(String name){
this.name = name;
}
}
class Chunk{
Entity[] entity;
String name;
Chunk(){
}
Chunk(String name){
this.name = name;
entity = new Entity[2];
Entity e1 = new Player();
Entity e2 = new Player();
e1.setName(name);
e2.setName("Dummy");
entity[0] = e1;
entity[1] = e2;
}
public Entity[] getEntities() {
return entity;
}
}
class Player extends Entity{
}
public class MapsT {
public void checkMap(){
Map<Chunk, List<Player>> chunkListMap = new HashMap<>();
List<Chunk> affectedChunks = new ArrayList<Chunk>();
Chunk ch = new Chunk("Z");
affectedChunks.add(0, ch);
affectedChunks.add(1, ch);
affectedChunks.add(2, new Chunk("A"));
affectedChunks.add(3, new Chunk("B"));
affectedChunks.add(4, new Chunk("C"));
affectedChunks.add(5, new Chunk("D"));
for (Chunk c : affectedChunks) {
List<Player> currentPlayers = playersInChunk(c);
if (!chunkListMap.containsKey(c)) {
chunkListMap.put(c, currentPlayers);
continue;
}
List<Player> previousPlayers = chunkListMap.get(c);
List<Player> enteringPlayers = new ArrayList<>(currentPlayers);
enteringPlayers.removeAll(previousPlayers);
//for (Player player : enteringPlayers) applyEffects(player);
List<Player> leavingPlayers = new ArrayList<>(previousPlayers);
leavingPlayers.removeAll(currentPlayers);
//for (Player player : leavingPlayers) unapplyEffects(player);
}
}
private List<Player> playersInChunk(Chunk c) {
List<Player> retVal = new ArrayList<>();
for (Entity e : c.getEntities()) {
if (e instanceof Player) retVal.add((Player) e);
}
return retVal;
}
public static void main(String[] args) {
MapsT mapt = new MapsT();
mapt.checkMap();
}
}