private static int fieldCompare(String key, String[] keys) { return Objects.compare(key, keys, new Comparator<Object>() { public int compare(Object o1, Object o2) { String key1 = null; String[] keys1 = null; if (o1 instanceof String) { key1 = (String) o1; } if (o2 instanceof String[]) { keys1 = (String[]) o2; } if (key1 == null || keys1 == null) { return -1; } for (String m : keys1) { if (m.equals(key1)) { return 0; } } return -1; } }); }
開源google的guava包下,Maps,Sets系列工具類,有contains(Key)與上述功能相同,簡潔代碼,效率應該也更高,學無止境,幸得高人指點。java