如何将HashMap的键与String进行比较?
我的文本文件有100k行,并将其放入HashMap。
例如,我的HashMap如下所示:
{test1=1}
{test2=2}
up to...
{test100000=100000}
另一方面,我正在阅读一百万行文本文件。 文本文件包含以下数据:
test1,first,input1
test2,second,input2
up to..
test1000000,1million,input1million
我正在用“,”将它分成一行,而我只是得到该行的第一个数据。这是“测试词”,例如:
test1
test2
所以我想要做的是,我想检查我的HashMap的键是否存在于文本文件中。
我的问题是,我的其他文本文件比我的HashMap的行大,所以它可能会抛出NullPointerException或NoSuchElement。
这是我的代码:
public static void main(String[] args) {
File small = new File("C:\test\\testfolder\\small.txt"); // text file (100k+lines) put in hashmap
File large = new File("C:\test\\testfolder\\big.txt"); // text file (1million+ lines) just read
ArrayList<String> smallData= new ArrayList();
smallData.addAll(getData(small));
Map<String,String> smallMap = new HashMap();
smallMap = MapSmallFile(smallData);
try{
LineIterator it = FileUtils.lineIterator(large,"UTF-8");
String line;
String[] large_data;
while(it.hasNext()){
line = it.nextLine();
large_data = StringUtils.split(line, (","));
//do the comparing here
if(large_data[0].equalsIgnoreCase(?????)
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
private static ArrayList<String> getData(File file) {
ArrayList<String> data = new ArrayList();
String line;
try{
LineIterator it = FileUtils.lineIterator(file,"UTF-8");
while(it.hasNext()){
line = it.nextLine();
data.add(line);
}
it.close();
}
catch(Exception e){
e.printStackTrace();
}
return data;
}
private static Map<String,String> MapSmallFile(ArrayList<String> inputlist){
String[] data;
Map<String,String> hashmap = new HashMap<String,String>();
for(int i=0; i<inputlist.size(); i++){
data = inputlist.get(i).split(",");
hashmap.put(data[0], data[1]);
}
return hashmap;
}
答案 0 :(得分:0)
最好使用HashMap的布尔containsKey(Object key)方法,而不是在main()中直接调用equalsIgnore ..()。 如有必要,您可以创建自己的类实现接口Map,并使其成为HashMap类型字段的委托者,以便自定义管理密钥比较。(您可以为键重写equals()和hashCode()。项目8和项目9在Josh Bloch的Effective Java 2nd ed。中将为您提供详细指南。)
答案 1 :(得分:0)
我不确定这是否适合您,但如果您使用的是Java 8,则可以大大减少代码和依赖项的数量。
此外,如果您想进行不区分大小写的比较,在插入/搜索地图之前,可能需要考虑在键上调用toLowerCase()
。
以下是您可以使用的一些可能的Java 8代码:
public static void main(String[] args)
{
// text file (100k+lines) put in hashmap
Path small = Paths.get("C:\\test\\testfolder\\small.txt");
// text file (1million+ lines) just read
Path large = Paths.get("C:\\test\\testfolder\\big.txt");
try
{
Map<String, String> smallMap = Files.lines(small, StandardCharsets.UTF_8)
.map(s -> s.split(","))
.collect(Collectors.toMap(ss -> ss[0].toLowerCase(), ss -> ss[1]));
Files.lines(large, StandardCharsets.UTF_8)
.map(s -> s.split(",")[0].toLowerCase())
.filter(s -> smallMap.containsKey(s))
.forEachOrdered(s -> processData(s, smallMap.get(s)));
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void processData(String key, String value)
{
// Do what needs to be done with the matching key/value
}