java - 使用 compareToIgnoreCase (JShell) 对文件流进行排序时出现“找不到符号”
问题描述
在Debian Stretch上使用java 13.0.1的JShell :
import static java.nio.file.Files.*;
var hm=Path.of(System.getProperty("user.home"));
void p(String s) {System.out.println(s);}
list(hm).sorted((a,b)->a.compareTo(b)).forEach(x -> p(x.toString().substring(hm.toString().length()+1)));
list(hm).sorted().forEach(x -> p(x.toString().substring(hm.toString().length()+1)));
"a".compareToIgnoreCase("A");
这一切都有效(它列出了主文件夹中的文件两次,并返回 0)。
但是当我输入时:
list(hm).sorted((a,b)->a.compareToIgnoreCase(b)).forEach(x -> p(x.toString().substring(hm.toString().length()+1)));
它会导致错误:
找不到符号,符号:方法compareToIgnoreCase。
知道是什么让compareToIgnoreCase失败了吗?
解决方案
hm
是 a Path
,因此list(hm)
返回 a Stream<Path>
。compareToIgnoreCases(...)
类中没有方法Path
。如果想使用compareToIgnoreCase
fromString
,则需要先将 to 转换Path
,String
例如通过调用toString()
:
list(hm)
.sorted((a,b) -> a.toString().compareToIgnoreCase(b.toString()))
.forEach(x -> p(x.toString().substring(hm.toString().length() + 1)));
查看流链中所做的事情,将条目映射到String
进一步执行之前似乎是明智的:
list(hm)
.map(Path::toString) // convert Stream<Path> to Stream<String>
.sorted(String::compareToIgnoreCase)
.map(x -> x.substring(hm.toString().length() + 1))
.forEach(this::p);
(上面的代码假设它p
是this
-object的一个实例方法)
推荐阅读
- python - When i insert the data using PyQt5 and UI, i got the error "Python has stopped working"
- hadoop - Unable to create hive table with constraints in Hive 2.3.0
- html - 使用 vba 将 ie 放在前面并激活它(即手动单击)
- github - 实现自定义域名
- c# - Window Loaded & ContentRendered 事件的问题
- python - 使用 opencv 和 gtk 添加按钮暂停停止记录
- python - 关于 Python 中的数组(Numpy)
- php - 如何在codeigniter中实现db close
- c - 分段错误 - 试图将二进制文件读入内存
- python - Python用特殊字符解密字节