// Example 122 from page 97 of Java Precisely second edition (The MIT Press 2005) // Author: Peter Sestoft (sestoft@itu.dk) import java.util.*; // The HashSet is 2 to 5 times faster than Arrays.binarySearch, even // for this list of strings of only 52 elements: class Example122 { final static String[] keywordarray = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" }; final static Set keywords = new HashSet(Arrays.asList(keywordarray)); static boolean isKeyword1(String id) { return keywords.contains(id); } static boolean isKeyword2(String id) { return Arrays.binarySearch(keywordarray, id) >= 0; } public static void main(String[] args) { if (args.length != 2) System.out.println("Usage: java Example122 "); else { final int count = Integer.parseInt(args[0]); final String id = args[1]; for (int i=0; i