If you use the regular expression only once, its probability will be slower, especially if you use find , where you really mean matches , as in your question.
When you save a compiled Pattern and use it several times, it can be faster than several peers.
This, however, depends on the context. If all the String tags you are testing are literals, and matches are more likely than unsuccessful, String.equals will be much faster since it tests the loop if the instances are the same.
The quickest solution is to pre-select, based on the easily verifiable property of the String candidate, which is different for all String s, for example. a char at a specific position or length , and do one equals for the selected String . In your case, both, the first character and the length are appropriate. In this case, I prefer length, since you need to check the length in any case before accessing the character to protect against empty Strings :
Preferred option using length :
nomatch: { switch(modoImpressao.length()) { case 3: if(modoImpressao.equals("PDF")) break nomatch; break; case 5: if(modoImpressao.equals("AMBOS")) break nomatch; break; case 10: if(modoImpressao.equals("IMPRESSORA")) break nomatch; break; } throw new EspdNeverStopParametroInvalidoException( TspdConstMessages.IMPRIMIR_PARAMETRO_MODOIMPRESSAO_INVALIDO, "TspdImprimirNFCe"); }
Option using the first char :
nomatch: { if(modoImpressao.length()>0) switch(modoImpressao.charAt(0)) { case 'P': if(modoImpressao.equals("PDF")) break nomatch; break; case 'A': if(modoImpressao.equals("AMBOS")) break nomatch; break; case 'I': if(modoImpressao.equals("IMPRESSORA")) break nomatch; break; } throw new EspdNeverStopParametroInvalidoException(TspdConstMessages.IMPRIMIR_PARAMETRO_MODOIMPRESSAO_INVALIDO,"TspdImprimirNFCe"); }