@OverridepublicVput(Kkey,Vvalue){if(key==null){returnputValueForNullKey(value);}//some code here// No entry for (non-null) key is present; create onemodCount++;if(size++>threshold){tab=doubleCapacity();index=hash&(tab.length-1);}addNewEntry(key,value,hash,index);returnnull;}
privatevoidprintFileByLine(StringfilePath){try{FileInputStreaminputStream=newFileInputStream("textfile.txt");BufferedReaderbr=newBufferedReader(newInputStreamReader(inputStream));StringstrLine;//Read File Line By Linewhile((strLine=br.readLine())!=null){// Print the content on the consoleSystem.out.println(strLine);}br.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}
如果我们进行文件是否存在的检查,抛出FileNotFoundException的概率会减少很多,
1234567891011121314151617181920
privatevoidprintFileByLine(StringfilePath){if(!newFile(filePath).exists()){return;}try{FileInputStreaminputStream=newFileInputStream("textfile.txt");BufferedReaderbr=newBufferedReader(newInputStreamReader(inputStream));StringstrLine;//Read File Line By Linewhile((strLine=br.readLine())!=null){// Print the content on the consoleSystem.out.println(strLine);}br.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}
上述的检查是一个不错的编码技巧,建议采纳。
不要过多创建线程
在android中,我们应该尽量避免在主线程中执行耗时的操作,因而需要使用其他线程。
123456789
privatevoidtestThread(){newThread(){@Overridepublicvoidrun(){super.run();//do some io work}}.start();}
publicclassMyApplicationextendsApplication{privatestaticfinalStringLOGTAG="MyApplication";@OverridepublicvoidonCreate(){StringcurrentProcessName=getCurrentProcessName();Log.i(LOGTAG,"onCreate currentProcessName="+currentProcessName);super.onCreate();if(getPackageName().equals(currentProcessName)){//init for default process}elseif(currentProcessName.endsWith(":network")){//init for netowrk process}}privateStringgetCurrentProcessName(){StringcurrentProcessName="";intpid=android.os.Process.myPid();ActivityManagermanager=(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);for(ActivityManager.RunningAppProcessInfoprocessInfo:manager.getRunningAppProcesses()){if(processInfo.pid==pid){currentProcessName=processInfo.processName;break;}}returncurrentProcessName;}}