Reputation: 5761
I have a class of objects. Ideally, I'd like to place them all into five separate sets. Then, I'd like to add all five of these sets to a HashMap and give them each a unique identifier.
Finally, I'd like to be able to search the HashMap not for the sets, but for the objects contained in the sets. Is this possible? I have looked up as much information as I can find about HashMaps but I can't seem to find a way of doing this. If I can't, does anybody have any good alternatives?
Edit - Sample code:
public class Cell{
private int x;
private int y;
public Example(int x, int y) {
this.x= x;
this.y= y;
}
}
Then, using MouseListener I detect where on a grid a user has clicked. I determine how many ships (am modelling a Battleship game) they have placed by making a method call to return how long the next ship to be placed it. I then do this:
for (int i = 0; i < currentShipLength; i++)
playerGridLogic.addCellsWithShips(new Cell(x, y+ i));
numberOfPlayerShipsPlaced++;
Finally, once all ships are placed, I'd like to add them to HashMap. Currently, I'm adding all ships to a large set. When the computer clicks on a cell in my grid, the following is executed:
if (playerGridLogic.getCellsWithShips().contains(attackedCell)) {
playerGrid.getCellArray()[xPos][yPos].setBackground(Color.ORANGE);
}
else {
playerGrid.getCellArray()[xPos][yPos].setBackground(Color.MAGENTA);
}
Whilst this all works perfectly, it only tells me a ship has been hit. It doesn't tell me which one, which I'd like to know so I can output information to the user when that ship has been destroyed.
I thought the easiest way was creating five sets instead of just one, then adding them to a HashMap as explained above. However, I'm not sure if this is possible - my experiments haven't worked thus far.
Upvotes: 0
Views: 1172
Reputation: 626
Do you need to pull that object out of the set and do something with it or simply check that it is in there. If you only need to check if it is in there the following will for fine
boolean isInMap = false;
for(Set<MyObject> s: map.values()){
if(s.contains(objectToSearchFor)){
isInMap=true;
break;
}
}
If you need to actually get the object to act on it, inside the if statement you can look through the set and check if each one matches what you are looking for.
Upvotes: 0
Reputation: 3986
Create a wrapper object for the objects that now reside in your sets.
example
interface IdentifiableWrapper {
Object setObject(); // this is the object you want to search
Object id(); // id that you generate and assign to each object and later use to search
}
now add instances of this object to your hashmap and search them. I have skipped mentioning the guidelines like the key/ id in the hashmap should be immutable et-all.
Upvotes: 0
Reputation: 94499
Here is a quick example I wrote up.
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Example{
public static void main(String[] args) {
MyObject obj = new MyObject("Kevin");
MyObject obj1 = new MyObject("Chris");
MyObject obj2 = new MyObject("Oliver");
MyObject obj3 = new MyObject("Jack");
MyObject obj4 = new MyObject("Joe");
Set<MyObject> set = new HashSet<MyObject>();
Set<MyObject> set1 = new HashSet<MyObject>();
Set<MyObject> set2 = new HashSet<MyObject>();
Set<MyObject> set3 = new HashSet<MyObject>();
Set<MyObject> set4 = new HashSet<MyObject>();
set.add(obj);
set1.add(obj1);
set2.add(obj2);
set3.add(obj3);
set4.add(obj4);
Map<String,Set<MyObject>> map = new HashMap<String,Set<MyObject>>();
map.put("s", set);
map.put("s1", set1);
map.put("s2", set2);
map.put("s3", set3);
map.put("s4", set4);
MyObject found = search(map, "Jack");
System.out.println(found.name);
}
private static MyObject search(Map<String, Set<MyObject>> map, String string) {
for(Set<MyObject> s: map.values()){
for(MyObject mObj:s){
if(mObj.name.equals(string)){
return mObj;
}
}
}
return null;
}
}
class MyObject{
String name;
public MyObject(String name){
this.name = name;
}
}
Upvotes: 1