开发者

Java - Select object by int

开发者 https://www.devze.com 2023-04-07 22:16 出处:网络
How do I select an object by the an int inside it? Here is code: String FolderName = result.getString (\"name\");

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)
      parent开发者_C百科Folder.addChildFolder(folder);
  }

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号