How iterate in Hashmap till the required object

234 Views Asked by At

i am doing programming in GWT

i have class FolderColection and Folder, in FolderColection Class there is Hashmap hm

public class folderCollection{

  public Map<String,Folder>FolderCollection = new HashMap<String,Folder>();

  public void addFolder(Folder folder){
    String folderKey = folder.getKey();
    FolderCollection.put(folderKey, folder);
  }
}

In Folder class

public Class Folder{
  // Not a complete code , but an idea of a code 
  String name;
  String dateOfcreation;

  private FolderCollection  folderCollection = new FolderCollection();
  // Folder can also have many sub folders
  //More variables
  // all get set methods 
}

Now for example : all are folders

 1.A
    1.Aa
      1.Aa1
      2.Aa2
    2.Ab
 2.B
    1.Ba
    2.Bb
 3.C

A, B , C Folders are in FolderCollection. As A is also a folder and it contains FolderCollection (Folder Aa , Folder Ab). Similarly Folder Aa has FolderCollection of (Folder Aa1, Folder Aa2).

I able to make this which i have explained above.

I have difficulty in Accesing the object for example object of Folder Aa1. Suppose I want to change the name of Folder Aa2, for that i have to iterate till that object.

Please Help me to solve this.

i have path as all the folder names are added in tree widget accordint to the parentsTreeItem.

ex: if i have to change name of Aa2 then I have

 String [] path ={A,Aa,Aa2};

Some Help me with this will be of great Help.

2

There are 2 best solutions below

0
On BEST ANSWER

If you have the path you don't have to iterate through the hashmap, just get the value for each path element from the respective folder collection.

3
On

I expect you're going to have to have a getFolder(?) on your Folder class that you provide the folder name to and it returns the sub-Folder with that name. You can use your path to traverse this in a loop to "cd" to the level you're interested in. Then, I guess a getFile will return a File object. You can then rename your file (though your path would have to be updated to reflect any Folder name changes).

Folder rootFolder = ...;
String[] path = ...;
Folder f = null;
for (int i=0; i<path.length; i++) {
    folder = rootFolder.getFolder(path[i]);
}

// f now contains the folder at "path"
// N.B. Haven't handled FolderNotFoundException
File file = f.getFile("Aa2.txt");
file.setName("Aa2.new.txt");

/* Folder#getFolder returning sub-folder for name passed */
public Folder getFolder(String name) {
    Folder f = folderCollection.get(name);    //no null checks done! 
    if (f == null) {
        ... //FolderNotFoundException?
    }
    return f;
}

Alternatively, you could provide the path (or an encapsulated representation of path) to Folder#getFolderInPath and handle it all internally to Folder. This version is iterative, looping over the path array, updating f with that level in the folder path on each iteration. It uses getFolder from the above:

Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");

/* Folder#getFolder returning sub-folder for name passed 
 * This is an "iterative" implementation - looping path array
 */
public Folder getFolderInPath(String[] path) {
    Folder f = null;
    for (int i=0; i<path.length; i++) {
        folder = this.getFolder(path[i]);
    }
    // f now contains the folder at "path"
    // N.B. Haven't handled FolderNotFoundException
    return f;
}

This is a recursive version of the above and uses getFolder (above x 2) again. Needs some explaining - #getFolderInPath now just delegates to the recursive method #getFolderRecursive, passing the root values of the recursion (the starting folder ["this"] and the start point in path - index).

Method #getFolderRecursive(Folder recursively calls itself until the path is traversed (while index < path.length). When it is traversed successfully (FolderNotfoundException?) then index = path.length. At each level, we make the recursive call with the folder found matching the name at that level in the path (governed by index). At the bottom level, the folder found with #getFolder is the last in the path and should be returned. The recursive methods are then unravelled, passing f up the tree in var folderArtPath (for clarity). The else condition sets this variable when the path is traversed and it is carried up the stack in the line folderAtPath = in the if block:

Folder rootFolder = ...;
String[] path = ...;
Folder f = rootFolder.getFolderInPath(path);
File file = f.getFile("Aa2.txt");

/* Folder#getFolder returning sub-folder for name passed 
 * This is an "recursive" implementation - digging into the Folder structure
 */
public Folder getFolderInPath(String[] path) {
    //Begin recursion with "this2 folder"
    return getFolderRecursive(this, path, 0);
}

/* Internal recursive method
 */
private Folder getFolderRecursive(Folder baseFolder, String[] path, int index) {

    Folder folderAtPath = null; //This is going to carry the Folder at "path"

    if (index < path.length) {  //Recursive base condition (are we done?)

        //Get folder f with name according to path and index
        Folder f = baseFolder.getFolder(path[index]));    //FolderNotFoundException?

        //Recursively call found folder f with path and index referring
        //to next path-part to be used (index+1)
        folderAtPath = getFolderRecursive(f, path, index+1);
    }
    else {
        folderAtPath = baseFolder;
    }

    return folderAtPath;
}

There may be a better way to do this but I can't examine it now. Spent a bit too much time on it but had to correct my mini-cook-up. Recursion's a bit of fun... find a simple example on line and just play with it.

You also may want to make Folder and File have some common interface.