java - 如何在 Hashmap 中迭代直到所需的对象

标签 java gwt gwt2

我正在用 GWT 编程

我有类 FolderColection 和文件夹, 在 FolderColection 类中有 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);
  }
}

在文件夹类中

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 
}

现在举个例子:都是文件夹

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

A、B、C 文件夹在 FolderCollection 中。 因为 A 也是一个文件夹,它包含 FolderCollection(文件夹 Aa、文件夹 Ab)。 同样,文件夹 Aa 具有 FolderCollection of (Folder Aa1, Folder Aa2)。

我能够做到这一点,我在上面已经解释过了。

我在访问对象时遇到困难,例如文件夹 Aa1 的对象。 假设我想更改文件夹 Aa2 的名称,因为我必须迭代直到那个对象。

请帮我解决这个问题。

我有路径,因为所有文件夹名称都添加到 parentsTreeItem 的树小部件中。

例如:如果我必须更改 Aa2 的名称,那么 我有

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

有人帮我解决这个问题会有很大的帮助。

最佳答案

我预计您将必须在您的 Folder 类上有一个 getFolder(?),您向其提供文件夹名称并返回具有该名称的子文件夹。您可以使用您的路径在循环中遍历它以“cd”到您感兴趣的级别。然后,我猜 getFile 将返回一个 File 对象。然后您可以重命名您的文件(尽管您的路径必须更新以反射(reflect)任何文件夹名称更改)。

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;
}

或者,您可以向 Folder#getFolderInPath 提供路径(或路径的封装表示),并在内部将其全部处理到 Folder。这个版本是迭代的,遍历路径数组,在每次迭代时用文件夹路径中的那个级别更新 f。它使用上面的 getFolder:

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;
}

这是上述的递归版本并再次使用 getFolder(高于 x 2)。需要一些解释 - #getFolderInPath 现在只委托(delegate)给递归方法 #getFolderRecursive,传递递归的根值(起始文件夹 ["this"] 和起始点在路径 - 索引中)。

方法#getFolderRecursive(Folder递归调用自身,直到遍历路径(while index < path.length)。当遍历成功(FolderNotfoundException?)则index = path.length。在每个级别,我们使用找到的与路径中该级别的名称匹配的文件夹进行递归调用(由索引控制)。在最底层,使用 #getFolder 找到的文件夹是路径中的最后一个并且应该被返回。然后解开递归方法,将 f 传递到 var folderArtPath 中的树中(为清楚起见)。else 条件在遍历路径时设置此变量并且它在 if block 中的 folderAtPath = 行中被带入堆栈:

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;
}

可能有更好的方法来执行此操作,但我现在无法检查它。花了太多时间,但不得不纠正我的迷你 cooking 。递归有点有趣……在网上找一个简单的例子,然后玩一下。

您可能还希望使文件夹和文件具有一些通用接口(interface)。

关于java - 如何在 Hashmap 中迭代直到所需的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988877/

相关文章:

java - HTTP 身份验证和 SOAP 请求

java - 如何控制 GWT 中的执行流程?

css - 如何使用 UiBinder 声明依赖样式名称

javascript - 如何使用@JsInterop在GWT应用程序中公开增量DOM库的JS补丁功能

java - Hibernate 实体的最佳单元测试

java - Askey Android 电视盒上的 ExoPlayer 直播卡住,音频继续

java - 从 .txt 逐行读取填充 JList

gwt - Spring Boot + GET 嵌入式配置

java - 无法调试 gwt 2.8 和 java 1.8 版本的 gwt 测试

java - 在 GWT/GXT 中编译后更改 .properties 文件吗?