# Iterating through map : need help getting cluster of flags (points) while avoiding duplicates

I have a grid style game where I make use of objects called flags which have points attached to them.

I want to be able to look at the map and find the tile(position) where it maximizes the points (using the cardinal flags North,South,West,East of current position and uses radius to give a bigger search area); while trying to avoid using the same flags.

My method works in the sense that it adds the points up correctly based on the position. However, when I move to the next tile over; if the previous tile uses flags (x,y) and the next tile over uses flags (x,y,z)

Example using method below:

Tile 1 @ Position (3,4)

• Flags (x,y)

Tile 2 @ Position (3,5)

• Flags (x,y,z)

Expected result for Tile 2 : z flag only

    //searches each tile for corresponding flag points
public Map<Map<Integer, List<Flag>>, Position> checkTilePoints(Player player) {
List<Flag> localFlags = new ArrayList<>();
Map<Map<Integer, List<Flag>>, Position>tilePoints = new HashMap<>();
int diameter = 4; //as much diameter as warfrig
int radius = diameter / 2;
int squareRadius = radius;
for (int i = 0; i < context.getMap().getMap().length; i++) {
for(int j = 0; j < context.getMap().getMap()[i].length; j++) {
localFlags.clear();
int pointTotal = 0;
if(context.getMap().isRock(i, j, player) || context.getMap().isOutOfBounds(i, j)) {
continue;
}
for (int x = i - squareRadius; x < i + squareRadius; x++) {
for (int y = j - squareRadius; y < j + squareRadius; y++) {
if((java.lang.Math.pow(x - i, 2) + java.lang.Math.pow(y - j, 2)) < java.lang.Math.pow(radius, 2)) {
Flag checkedFlag = context.getMap().getFlag(x, y);
if ((checkedFlag != null) && (!localFlags.contains(checkedFlag))) { // add once only
localFlags.add(checkedFlag);
}
}
}
}

Flag north = context.getMap().getFlag(i, j + radius);
Flag south = context.getMap().getFlag(i, j - radius);
Flag west = context.getMap().getFlag(i - radius, j);
Flag east = context.getMap().getFlag(i + radius, j);

if ((north != null) && (!localFlags.contains(north))) {
localFlags.add(north);
}
if ((south != null) && (!localFlags.contains(south))) {
localFlags.add(south);
}
if ((west != null) && (!localFlags.contains(west))) {
localFlags.add(west);
}
if ((east != null) && (!localFlags.contains(east))) {
localFlags.add(east);
}

for(Flag points : localFlags) {
pointTotal += points.getSize().getID();
}
if(pointTotal != 0) {
//TODO - create hashmap to return
}
}
}
return null;
}


I'm not sure if I explained my problem very well, but overall; I want to fix this method to just add the cluster of flags based on the position without using duplicate flag already checked

## 1 Answer

Use a Java Set<T>. Even if you add duplicates to the Set, they will not be stored by the data structure.

This will avoid having to use any explicit conditionals in your code to avoid adding / checking duplicates.

• Set is an interface, not a class. Which one of the implementing classes would you recommend in this case and why? Jan 28 at 9:32