Archive for the ‘Java’ Category

Source: Rackspace

Advertisements

Java check for alpha numeric

Posted: September 30, 2010 in Java, Programming
Tags: ,
  1. if (“!abc123”.matches(“[a-zA-Z]*”)) {
  2. System.out.println(“Pure alphabetic string”);
  3. } else {
  4. System.out.println(“Not pure alphabets”);
  5. }

Java utility method to sort a Map based on map values.

@SuppressWarnings(value = { “unchecked” })
public static List sortByValue(final Map m) {
List keys = new ArrayList();
keys.addAll(m.keySet());
Collections.sort(keys, new Comparator() {
public int compare(Object o1, Object o2) {
Object v1 = m.get(o1);
Object v2 = m.get(o2);
if (v1 == null) {
return (v2 == null) ? 0 : 1;
} else if (v1 instanceof Comparable) {
return ((Comparable) v2).compareTo(v1);
} else {
return 0;
}
}
});
return keys;
}