我在其他语言中发现了这个问题,但还没有在java应用程序中找到解决这个问题的方法.
我有一个.txt包含数百万条记录的大文件.每条记录都是/n分隔的.基本上它是来自表的单列数据.目标是从输入文件中读取数据并对其进行分区.然后将分区数据写入新文件.例如,一个包含200万条记录的文件将成为200个文件,每个文件有10,000条记录(最后一个文件包含<10,000个.)
我成功地读取和分区数据.我成功创建了第一个文件,并且正确命名.
问题是只创建了一个文件,它是空的.代码编译和运行没有错误或异常.
我的代码如下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class ChunkTextFile {
private static final String inputFilename = "inputFile.txt";
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter fileWriter = null;
BufferedWriter lineWriter = null;
StringWriter stringWriter = null;
// Create an ArrayList object to hold the lines of input file
List<String> lines = new …Run Code Online (Sandbox Code Playgroud) 为什么Collectors在Java 8中类是final?我想在自己的课堂上扩展Collectors课堂MyCollectors。我将在其中添加Collectors类中不存在的其他方法。
我有以下代码:
fieldsToFilter.stream()
.map(e -> Arrays.asList(
Filters.ne(e, ""),
Filters.exists(e, true)
))
.toArray(Bson[]::new))
Run Code Online (Sandbox Code Playgroud)
在执行时我得到了 java.lang.ArrayStoreException: java.util.Arrays$ArrayList
Filters.exists并Filters.ne返回新的Bson实例.查看文档.
任何帮助表示赞赏
private void createAnagramSignatures() {
for (String word : dictionary.keySet()) {
int signature = getSignature(word);
if (!anagramMap.containsKey(signature)) {
anagramMap.put(signature, new ArrayList<String>());
}
anagramMap.get(signature).add(word);
}
}
Run Code Online (Sandbox Code Playgroud)
我对map.getOrDefault有点熟悉,只是不确定如何在以下情况下应用它。
可以说我有一个带有以下签名的方法:
Map<String, String> merge (String[][] ... maps)
Run Code Online (Sandbox Code Playgroud)
它使用(可变)数量的二维映射数组来表示映射,例如:
String[][] m1 = new String[][] {
{"k1", "a"},
{"k2", "b"}
};
String[][] m2 = new String[][] {
{"k1", "a"},
{"k2", "b"},
{"k3", "c"}
};
String[][] m3 = new String[][] {
{"k1", "x"},
{"k2", "b"}
};
Run Code Online (Sandbox Code Playgroud)
现在,我可以像这样从左到右合并任意两个地图:
Map<String, String> m1 = Stream.of(map1).collect(Collectors.toMap(k -> k[0], v -> v[1]));
Map<String, String> m2 = Stream.of(map2).collect(Collectors.toMap(k -> k[0], v -> Optional.ofNullable(v[1]).orElse("null")));
m1.forEach((key, value) -> m2.merge(key, value, (v1, v2) -> v1));
Run Code Online (Sandbox Code Playgroud)
但是我该如何合并各种数组映射的杂散数,以便在m1, m2, m3按照上面的定义通过之后,结果是:
String[][] m3 …Run Code Online (Sandbox Code Playgroud) 假设我有一个HashMap作为Map<Integer, List<Integer>> map = new HashMap<>();。
现在List<Integer> values = computeIfAbsent(key, ArrayList::new);可以正常工作,但是List<Integer> values = computeIfAbsent(key, LinkedList::new);会引发编译错误。
我可以在ArrayList和LinkedList中看到no-arg构造函数。我在这里想念的是什么,有人可以解释这种现象吗?
我有一个方法:
invokList(List<Object> list);
Run Code Online (Sandbox Code Playgroud)
此方法在jar中,我无法访问它的源代码。因此,我需要以并行方式执行invokList,有人可以为此提供帮助吗?
想法是将列表拆分为多个列表,然后并行执行invokList。
我做了这个例子:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
list.parallelStream()
.map(Collections::singletonList)
.forEach(Test::invokList);
}
public static void invokList(List<Integer> list) {
try {
Thread.sleep(100);
System.out.println("The Thread :" + Thread.currentThread().getName() + " is processing this list" + list);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 比方说,我有Employee类,它具有正确覆盖的equals和hashcode方法。
public class Employee {
private int eno;
private String firstName;
private String lastName;
@Override
public int hashCode() {
System.out.println("hashcode called");
final int prime = 31;
int result = 1;
result = prime * result + eno;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("equals called");
if (this == obj)
return true; …Run Code Online (Sandbox Code Playgroud) 我有List<List<Object>>类型数据。我需要在“列表”列表中添加相同的值。我尝试了以下方法,但由于它的类型不起作用List<Boolean>。
List<List<Object>> data = ...;
data.stream().map(v -> v.add("test")).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到相同的类型List<List<Object>>?
我有以下数据:
"data": [
[
5,
"Johnny",
"Lollobrigida"
],
[
6,
"Bette",
"Nicholson"
],
[
7,
"Grace",
"Mostel"
],
[
8,
"Matthew",
"Johansson"
]
]
Run Code Online (Sandbox Code Playgroud)
我想将其更改为:
"data": [
[
5,
"Johnny",
"Lollobrigida",
"test"
],
[
6,
"Bette",
"Nicholson",
"test"
],
[
7,
"Grace",
"Mostel" ,
"test"
],
[
8,
"Matthew",
"Johansson",
"test"
]
]
Run Code Online (Sandbox Code Playgroud) 我有一个Map<String, Set<String>>。我希望Set<String>使用streams API 在新的地图中获取所有值。我能够得到一个Set<Set<String>>,但是我想要的是将我的地图中所有集合的所有值放在一个集合中。使用流有可能吗?
提前致谢。
java ×10
java-8 ×10
java-stream ×5
dictionary ×2
bson ×1
collections ×1
collectors ×1
set ×1
stringwriter ×1