我试图使用以下代码将2D int数组转换为2D String数组:
Arrays.stream(intArray).map(a ->
Arrays.stream(a).map(i ->
Integer.toString(i)).toArray()).toArray(String[][]::new);
Run Code Online (Sandbox Code Playgroud)
但是cannot convert from String to int在做的时候我得到了编译时的错误Integer.toString(i).我以为可能是因为我正在收集int数组中流式传输数据的结果String,但是没有map创建新的Collection?
我正在对非常大的每个元素应用操作LinkedList<LinkedList<Double>>:
list.stream().map(l -> l.stream().filter(d ->
(Collections.max(l) - d) < 5)
.collect(Collectors.toCollection(LinkedList::new))).collect(Collectors.toCollection(LinkedList::new));
Run Code Online (Sandbox Code Playgroud)
在我的计算机(四核)上,并行流似乎比使用顺序流更快:
list.parallelStream().map(l -> l.parallelStream().filter(d ->
(Collections.max(l) - d) < 5)
.collect(Collectors.toCollection(LinkedList::new))).collect(Collectors.toCollection(LinkedList::new));
Run Code Online (Sandbox Code Playgroud)
然而,并不是每台计算机都是多核的。我的问题是,在单处理器计算机上使用并行流会比使用顺序流明显慢吗?
我想修改两个字符字符串中,例如在改变'i'成'e',每'e'到'i'这样的文字一样"This is a test"会成为"Thes es a tist".
我已经找到了一个有效的解决方案,但它很无聊且不优雅:
String input = "This is a test";
char a = 'i';
char b = 'e';
char[] chars = input.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(chars[i] == a) {
chars[i] = b;
}else if(chars[i] == b) {
chars[i] = a;
}
}
input = new String(chars);
Run Code Online (Sandbox Code Playgroud)
如何使用正则表达式实现这一目标?