Java集合查询:创建mkdir命令的数组

时间:2013-09-06 14:27:40

标签: java collections

我有LinkedHashSet String如下:

Set<String> dirSet = new LinkedHashSet<String>();

以上String中有12个Set元素,如下所示:

root/dir1/dir2/dir3/dir4/dir5/baseDir1/
root/dir1/dir2/dir3/dir4/dir5/baseDir1/subDir/
root/dir1/dir2/dir3/dir4/dir5/baseDir2/
root/dir1/dir2/dir3/dir4/dir5/baseDir2/subDir/
root/dir1/dir2/dir3/dir4/dir5/baseDir3/
root/dir1/dir2/dir3/dir4/dir5/baseDir3/subDir/
root/dir1/dir2/dir3/dir4/dir5/baseDir4/
root/dir1/dir2/dir3/dir4/dir5/baseDir4/subDir/
root/dir1/dir2/dir3/dir4/dir5/baseDir5/
root/dir1/dir2/dir3/dir4/dir5/baseDir5/subDir/
root/dir1/dir2/dir3/dir4/dir5/baseDir6/
root/dir1/dir2/dir3/dir4/dir5/baseDir6/subDir/

我想编写一个迭代上面Set的方法,方法的输出是String Array,包含创建上述目录结构的命令。

输出String Array应包含String元素,如下所示:

mkdir root/
mkdir root/dir1/
mkdir root/dir1/dir2/
mkdir root/dir1/dir2/dir3/
mkdir root/dir1/dir2/dir3/dir4/
mkdir root/dir1/dir2/dir3/dir4/dir5/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir1/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir1/subDir/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir2/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir2/subDir/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir3/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir3/subDir/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir4/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir4/subDir/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir5/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir5/subDir/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir6/
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir6/subDir/

我能够达到dir5级别,然后通过迭代Set并获取baseDir1/subDir/方法的帮助来创建令牌,直到String.split()为止。

然而,当我再次升级(baseDir2,baseDir3等)时,我无法解决。

我怎样才能做到这一点?

感谢阅读!

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

如果你的意思是mkdir unix命令,那么你可以作弊并使用mkdir -p来创建必要的父目录。

mkdir -p root/dir1/dir2/dir3/dir4/dir5/baseDir1/
mkdir -p root/dir1/dir2/dir3/dir4/dir5/baseDir1/subDir/

等。或者只使用File.mkdirs()在Java中创建目录,这样做会做同样的事情。

要回答实际问题,我会使用中间结构,例如TreeSet

TreeSet<String> mkdirCmds = new TreeSet<String>();
for(String origPath : dirSet) {
  if(origPath.endsWith("/")) {
    // strip trailing slash if there is one
    origPath = origPath.substring(0, origPath.length() - 1);
  }
  mkdirCmds.add("mkdir " + origPath);
  int lastSlash = origPath.lastIndexOf('/');
  while(lastSlash >= 0) {
    mkdirCmds.add("mkdir " + origPath.substring(0, lastSlash));
    lastSlash = origPath.lastIndexOf('/', lastSlash - 1);
  }
}
String[] result = mkdirCmds.toArray(new String[0]);

这将为您提供所有原始路径及其父母,祖父母等的mkdir命令列表,其中没有重复项(因为mkdirCmds是一个集合)并且父母在他们的孩子面前列出(因为TreeSet<String>按字典顺序排序)。

对于您的原始输入,它将生成

mkdir root
mkdir root/dir1
mkdir root/dir1/dir2
mkdir root/dir1/dir2/dir3
mkdir root/dir1/dir2/dir3/dir4
mkdir root/dir1/dir2/dir3/dir4/dir5
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir1
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir1/subDir
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir2
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir2/subDir
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir3
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir3/subDir
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir4
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir4/subDir
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir5
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir5/subDir
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir6
mkdir root/dir1/dir2/dir3/dir4/dir5/baseDir6/subDir

答案 2 :(得分:0)

这将有效,

List<String> list = Arrays.asList(
"root/dir1/dir2/dir3/dir4/dir5/baseDir1/",
"root/dir1/dir2/dir3/dir4/dir5/baseDir1/subDir/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir2/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir2/subDir/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir3/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir3/subDir/",
"root/dir1/dir2/dir3/dir4/dir5/baseDir4/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir4/subDir/",
"root/dir1/dir2/dir3/dir4/dir5/baseDir5/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir5/subDir/", 
"root/dir1/dir2/dir3/dir4/dir5/baseDir6/",
"root/dir1/dir2/dir3/dir4/dir5/baseDir6/subDir/");

Set<String> dirSet = new LinkedHashSet<>(list); 
Set<Path> allDirs = new LinkedHashSet<>();
for (String file : dirSet) {
    recur(allDirs, Paths.get(file));
} 

for (Path path : allDirs) {
    System.out.println("mkdir " + path);
}

...

public static void recur(Set<Path> paths, Path path) {
    if (path == null)
        return;
    recur(paths, path.getParent());
    paths.add(path);
}

使用递归方法。如果传递的Pathnull,则返回其他父项。然后添加路径。

因为我们使用保留顺序的LinkedHashSet,所以添加的第一个路径将是root的{​​{1}}目录,然后是其中的每个子目录。您也可以使用Path

打印

TreeSet

您可以随时订购。 mkdir root mkdir root\dir1 mkdir root\dir1\dir2 mkdir root\dir1\dir2\dir3 mkdir root\dir1\dir2\dir3\dir4 mkdir root\dir1\dir2\dir3\dir4\dir5 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir1 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir1\subDir mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir2 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir2\subDir mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir3 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir3\subDir mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir4 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir4\subDir mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir5 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir5\subDir mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir6 mkdir root\dir1\dir2\dir3\dir4\dir5\baseDir6\subDir 也可能有内部排序逻辑。