JAVA-基于另一个CSV文件读取CSV文件
我正在努力使它正常工作。所以我有两个CSV文件。 这个呢 所以最重要的是。我有第二个CSV中的搜索词。在第一个CSV中,我还有SearchTerms,它应该作为一个“标签”工作。 我需要的是根据第二个CSV中使用的搜索词,将第一个CSV中的产品ID保存到列表JAVA-基于另一个CSV文件读取CSV文件,java,file,csv,parsing,datareader,Java,File,Csv,Parsing,Datareader,我正在努力使它正常工作。所以我有两个CSV文件。 这个呢 所以最重要的是。我有第二个CSV中的搜索词。在第一个CSV中,我还有SearchTerms,它应该作为一个“标签”工作。 我需要的是根据第二个CSV中使用的搜索词,将第一个CSV中的产品ID保存到列表。因此,当使用Akt(pan)时,将导出一组ID列表。如果有更多的Akt(pan)ID集,我想它们会作为数组保存到一个列表中 我试着用csvLoader读取它,并用lookup()方法搜索它 private final Map data=
private final Map data=Maps.newHashMap();
公共比较查找(字符串搜索术语){
返回data.get(searchTerm);
}
比较班在哪里
public class Comparison {
@Parsed(field = "ProductId1")
private String productId1;
@Parsed(field = "ProductId2")
private String productId2;
@Parsed(field = "ProductId3")
private String productId3;
@Parsed(field = "SearchTerm")
private String SearchTerm;
public String getProductId1() {
return productId1;
}
public String getProductId2(){
return productId2;
}
public String getProductId3(){
return productId3;
}
public List<String> getProductIds(){
List<String> ids = new ArrayList<>();
Collections.addAll(ids, productId1, productId2, productId3);
return ids;
}
公共类比较{
@已解析(field=“ProductId1”)
私有字符串productId1;
@已解析(field=“ProductId2”)
私有字符串productId2;
@已解析(field=“ProductId3”)
私有字符串productId3;
@已解析(field=“SearchTerm”)
私有字符串搜索项;
公共字符串getProductId1(){
退货产品ID1;
}
公共字符串getProductId2(){
返回产品ID2;
}
公共字符串getProductId3(){
返回产品ID3;
}
公共列表getProductId(){
列表ID=新的ArrayList();
addAll(id、productId1、productId2、productId3);
返回ID;
}
}
我的解决方案不好。每当我尝试使用lookup()方法时,都会不断收到NullPointerException
你有什么办法让它工作吗?Thank oyu问题在于HashMap中键的数据类型。根据您的数据,它应该是字符串,而不是列表
private final Map data=Maps.newHashMap();
公共比较查找(字符串搜索术语){
返回data.get(searchTerm);
}
然后返回的对象(类型化比较)将具有给定搜索项的所有产品ID
public class Comparison {
@Parsed(field = "ProductId1")
private String productId1;
@Parsed(field = "ProductId2")
private String productId2;
@Parsed(field = "ProductId3")
private String productId3;
@Parsed(field = "SearchTerm")
private String SearchTerm;
public String getProductId1() {
return productId1;
}
public String getProductId2(){
return productId2;
}
public String getProductId3(){
return productId3;
}
public List<String> getProductIds(){
List<String> ids = new ArrayList<>();
Collections.addAll(ids, productId1, productId2, productId3);
return ids;
}
private final Map<String, Comparison> data = Maps.newHashMap();
public Comparison lookup(String searchTerm) {
return data.get(searchTerm);
}