3

How do I select an object by the an int inside it?

Here is code:

String FolderName = result.getString ("name");
String FolderId = result.getString ("category_id");
String ParentId = result.getString ("parent_id");
int intFolderId = Integer.parseInt(FolderId);
int intParentId = Integer.parseInt(ParentId);
System.out.println( FolderId+" "+FolderName+" "+ParentId );

Map<Integer, Folder> data = new HashMap<Integer, Folder>();
Folder newFolder = new Folder(FolderName, intFolderId, intParentId);
data.put(newFolder.getId(), newFolder);

  for(Folder folder : data.values()) 
  {
    int parentId = folder.getParentFolderId();
    Folder parentFolder = data.get(parentId);
    if(parentFolder != null)
      parentFolder.addChildFolder(folder);
  }

How can I select an object with a parentID of 0 for example?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
WookooUK
  • 156
  • 9
  • 3
    What is `Select`? What is `Object`? What is `id`? – Miserable Variable Sep 24 '11 at 12:52
  • 1
    If the int variable is a "property" it should have an accessor or "getter" method. Until you tell us more and fill in the details, I'm guessing that you would want to loop through a collection of these objects then test for the presence of your int of interest by calling the accessor method. – Hovercraft Full Of Eels Sep 24 '11 at 12:54
  • I am sorry for not making sense, submitted before i had refined my question and put in full code snipped. I'm new here. – WookooUK Sep 24 '11 at 13:05
  • You're already getting the id inside the for loop: `int parentId = folder.getParentFolderId()`. So why not use an if statement to check that for the value of interest? As an aside in my limited experience, even though id's are often "numbers" they really aren't numeric and are often held as Strings rather than as ints. For example, when will you ever add or multiply an id? – Hovercraft Full Of Eels Sep 24 '11 at 13:08

1 Answers1

4

Your question is not very clear. You should edit it and try to better explain what you're trying to do.

I'll make the assumption that you have a collection of objects, and you want to select the object using some sort of an ID. If that is the case you can use a java.util.Map implementation to store your objects.

For example:

Map<Integer, MyObject> map = new HashMap<Integer, MyObject>();
map.put(object1.getId(), object1);
map.put(object2.getId(), object2);
//... and so on ...

And you can retrieve the objects using:

MyObject object = map.get(id);

EDIT:

How can I select an object with a parentID of 0 for example?

Maintain a list of children in each Folder. I think you are already doing that, since you have used an addChildFolder() method. Then you simply get the Folder with ID 0 from the map. Its list of children will have parentID 0.

The best way to organize your data structure will depend ultimately on what you're trying to do. You may find this interesting.

Community
  • 1
  • 1
Akshay
  • 1,606
  • 3
  • 17
  • 32
  • I am sorry it was not clear, Have been stressing out over the code and just came to stack.o.f and punched my keyboard :D have edited the text. – WookooUK Sep 24 '11 at 13:04
  • 2
    @user961574: this is a good suggestion, you might want to read up on maps. If you're going to be doing a lot of isolating items by their values, this technique will come in handy. 1+ – Hovercraft Full Of Eels Sep 24 '11 at 13:10