Java HashMap检查文件中是否存在

Java HashMap检查文件中是否存在,java,iterator,hashmap,Java,Iterator,Hashmap,如何将HashMap的键与字符串进行比较 我有一个10万行的文本文件,并把它放到HashMap中 例如,我的HashMap如下所示: {test1=1} {test2=2} up to... {test100000=100000} test1,first,input1 test2,second,input2 up to.. test1000000,1million,input1million 另一方面,我正在读取一个文本文件的一百万行。 文本文件包含如下数据: {test1=1} {test

如何将HashMap的键与字符串进行比较

我有一个10万行的文本文件,并把它放到HashMap中

例如,我的HashMap如下所示:

{test1=1}
{test2=2}
up to...
{test100000=100000}
test1,first,input1
test2,second,input2
up to..
test1000000,1million,input1million
另一方面,我正在读取一个文本文件的一百万行。 文本文件包含如下数据:

{test1=1}
{test2=2}
up to...
{test100000=100000}
test1,first,input1
test2,second,input2
up to..
test1000000,1million,input1million
我用“,”把它分成几行,我只得到这行的第一个数据。这是“测试词”,例如:

test1
test2
所以我想做的是,我想检查HashMap的键是否存在于文本文件中

我的问题是,我的另一个文本文件比HashMap的行大,因此可能抛出NullPointerException或NoTouchElement。

这是我的密码:

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;
    }
publicstaticvoidmain(字符串[]args){
File small=新文件(“C:\test\\testfolder\\small.txt”);//文本文件(100k+行)放入hashmap
File large=新文件(“C:\test\\testfolder\\big.txt”);//刚刚读取的文本文件(100多万行)
ArrayList smallData=新的ArrayList();
addAll(getData(small));
Map smallMap=newhashmap();
smallMap=MapSmallFile(smallData);
试一试{
LineIterator it=FileUtils.LineIterator(大,“UTF-8”);
弦线;
字符串[]大数据;
while(it.hasNext()){
line=it.nextLine();
大数据=StringUtils.split(行,(“,”);
//在这里进行比较
if(大数据[0]。相等信号情况(???)
}
}
捕获(例外情况除外){
例如printStackTrace();
}
}
私有静态ArrayList getData(文件){
ArrayList数据=新的ArrayList();
弦线;
试一试{
LineIterator it=FileUtils.LineIterator(文件“UTF-8”);
while(it.hasNext()){
line=it.nextLine();
数据。添加(行);
}
it.close();
}
捕获(例外e){
e、 printStackTrace();
}
返回数据;
}
私有静态映射映射文件(ArrayList inputlist){
字符串[]数据;
Map hashmap=新hashmap();

对于(int i=0;i而言,使用HashMap的boolean containsKey(Object key)方法似乎比直接调用main()中的equalsIgnore..()要好。
如有必要,您可以创建自己的类实现接口映射,并使其成为HashMap类型字段的委托者,用于自定义密钥比较管理。(您可以为密钥重写equals()和hashCode()。Josh Bloch的《有效Java第二版》中的第8项和第9项将为您提供详细的指南。)

我不确定这是否适合您,但是如果您使用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
  }
publicstaticvoidmain(字符串[]args)
{
//文本文件(100k+行)放入hashmap
Path small=Path.get(“C:\\test\\testfolder\\small.txt”);
//刚刚读取的文本文件(100多万行)
Path large=Path.get(“C:\\test\\testfolder\\big.txt”);
尝试
{
Map smallMap=Files.lines(小,标准字符集.UTF_8)
.map(s->s.split(“,”)
.collect(Collectors.toMap(ss->ss[0].toLowerCase(),ss->ss[1]);
Files.line(大的标准字符集.UTF_8)
.map(s->s.split(“,”[0].toLowerCase())
.filter->smallMap.containsKey
.forEachOrdered->processData,smallMap.get(s));
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
私有静态void processData(字符串键、字符串值)
{
//使用匹配的键/值执行需要执行的操作
}

Hash-map-get方法在key不存在时不会抛出异常,它将值返回为null。但问题是,我如何检查这些key是否存在于行中?@tututuyokgaming您可以使用
HashMap.keySet()
返回一个
列表
,并使用
List.contains
检查。@M.Shaw但根据该逻辑,我只是检查列表中的数据是否与行中的数据相同。但我想比较的是列表中是否存在行中的数据。