Java 如何在读取csv文件时跳过重复的行?
我需要跳过csv文件中的重复项,并需要将原始值插入我的数据库。 这是我的密码Java 如何在读取csv文件时跳过重复的行?,java,json,couchdb,Java,Json,Couchdb,我需要跳过csv文件中的重复项,并需要将原始值插入我的数据库。 这是我的密码 public class Json { public List<User> jsonConvert() throws JsonGenerationException, JsonMappingException, IOException { File input = new File("C:\\Users\\SwedhaS\\Documents\\Sam
public class Json {
public List<User> jsonConvert()
throws JsonGenerationException, JsonMappingException, IOException {
File input = new File("C:\\Users\\SwedhaS\\Documents\\SametimeFileTransfers\\newPIRs.csv");
Pattern pattern = Pattern.compile(",");
try (BufferedReader in = new BufferedReader(new FileReader(input));) {
List<User> users = in
.lines()
.skip(1)
.<User>map(line -> {
String[]x = new String[6];
x = pattern.split(line);
return new User(x);
}).collect(Collectors.toList());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValue(System.out, users);
return users;
}
}
}
公共类Json{
公共列表jsonConvert()
抛出JsonGenerationException、JsonMappingException、IOException{
文件输入=新文件(“C:\\Users\\SwedhaS\\Documents\\SametimeFileTransfers\\newPIRs.csv”);
Pattern=Pattern.compile(“,”);
try(BufferedReader in=new BufferedReader(new FileReader(input));){
列出用户=in
.行()
.skip(1)
.map(行->{
字符串[]x=新字符串[6];
x=图案。分割(线);
返回新用户(x);
}).collect(Collectors.toList());
ObjectMapper mapper=新的ObjectMapper();
enable(SerializationFeature.INDENT_输出);
mapper.writeValue(System.out,用户);
返回用户;
}
}
}
我相信您正在尝试从用户列表中删除重复项,对吗
为此,如果您还想保留顺序,可以使用哈希集,它会自动删除重复项:
Set<String> usersSet = new LinkedHashSet<>(users);
Set usersSet=newlinkedhashset(用户);
我相信您正在尝试从用户列表中删除重复项,对吗
为此,如果您还想保留顺序,可以使用哈希集,它会自动删除重复项:
Set<String> usersSet = new LinkedHashSet<>(users);
Set usersSet=newlinkedhashset(用户);
假设equals
和hashcode
在用户
中正确实现,尝试用集合
替换列表
(参见收集器.toSet()
)。假设equals
和hashcode
在用户
中正确实现,尝试将列表
替换为集合
(参见收集器.toSet()
)。