我得到詹金斯的编译错误:
不兼容的类型:
java.util.List<java.lang.Object>
无法转换为java.util.List<java.nio.file.Path>
在线:
collectList = dirList.collect(Collectors.toList());
在以下方法中:
public String getMostRecentFolder(String parentFolder){
List<Path> collectList = null;
Path path = null;
Path pathName = null;
Stream<Path> dirList = null;
try {
dirList = Files.list(Paths.get(parentFolder)).sorted(new Comparator<Path>() {
@Override
public int compare(Path path1, Path path2) {
Long file1Name = path1.toFile().lastModified();
Long file2Name = path2.toFile().lastModified();
return file2Name.compareTo(file1Name);
}
});
} catch (Exception e) {
Log.error("getMostRecentFolder failed with exception: " + e);
}
try {
collectList = dirList.collect(Collectors.toList());
} catch (Exception e) {
Log.error("getMostRecentFolder failed with exception: " + e);
}
try {
path = (Path) collectList.get(0);
} catch (Exception e) {
Log.error("getMostRecentFolder failed with exception: " + e);
}
try {
pathName = path.getFileName();
} catch (Exception e) {
Log.error("getMostRecentFolder failed with exception: " + e);
}
return pathName.toString().trim();
}
答案 0 :(得分:1)
试试这个。我没有收到此代码的错误。 我刚用&#34;替换了参数。&#34;和println的记录器使它在main方法中运行。您可以撤消这些更改。
List<Path> collectList = null;
Path path = null;
Path pathName = null;
Stream<Path> dirList = null;
try {
dirList = Files.list(Paths.get(".")).sorted((p1, p2) -> ((Long) p1.toFile().lastModified()).compareTo(p2.
toFile().lastModified()));
} catch (Exception e) {
System.out.println("err");
}
try {
collectList = dirList.collect(Collectors.toList());
} catch (Exception e) {
System.out.println("err");
}
try {
path = (Path) collectList.get(0);
} catch (Exception e) {
System.out.println("err");
}
try {
pathName = path.getFileName();
} catch (Exception e) {
System.out.println("err");
}
在Java 8中你可以使用lambdas。如果你必须在java 1.8之前使用某些版本 然后用这样的匿名内部类替换lambda表达式。
List<Path> collectList = null;
Path path = null;
Path pathName = null;
Stream<Path> dirList = null;
try {
dirList = Files.list(Paths.get(".")).sorted(new Comparator<Path>()
{
@Override
public int compare(Path p1, Path p2)
{
return ((Long) p1.toFile().lastModified()).compareTo(p2.
toFile().lastModified());
}
});
} catch (Exception e) {
System.out.println("err");
}
try {
collectList = dirList.collect(Collectors.toList());
} catch (Exception e) {
System.out.println("err");
}
try {
path = (Path) collectList.get(0);
} catch (Exception e) {
System.out.println("err");
}
try {
pathName = path.getFileName();
} catch (Exception e) {
System.out.println("err");
}