I have this code in a reserved word boolean format:
private boolean isIdent(String t) {
if (equals(t, "final") || equals(t, "int") || equals(t, "while")
|| equals(t, "if") || equals(t, "else") || equal开发者_如何学运维s(t, "print")) return false;
if (t!=null && t.length() > 0 && Character.isLetter(t.charAt(0))) return true;
else return false;
}
I need to turn this into a HashSet format but unsure how to approach this. Any help would be most appreciated.
You mean by putting the reserved words in a Set?
private Set<String> keywords;
private void initKeywords() {
keywords = new HashSet<String>();
keywords.add("final");
keywords.add("int");
keywords.add("while");
keywords.add("if");
keywords.add("else");
keywords.add("print");
}
private boolean isIdent(String t) {
if (keywords.contains(t)) {
return false;
}
else if (t != null && t.length() > 0 && Character.isLetter(t.charAt(0))) {
return true;
}
else {
return false;
}
}
精彩评论