Thursday, January 16, 2014

How to sort collection on multiple attributes



 


Q) How to sort collection on multiple attributes?
A) For classes that don’t implement
comparable interface, or when one needs even more control over ordering based on multiple attributes, a
Comparator interface should be used.


Example:

public class graduationCeremony {
    String campus;
    String faculty;
    String building;
}

public class GraduationCeremonyComparator implements Comparator<graduationCeremony> {
    public int compare(graduationCeremony o1, graduationCeremony o2) {
        int value1 = o1.campus.compareTo(o2.campus);
        if (value1 == 0) {
            int value2 = o1.faculty.compareTo(o2.faculty);
            if (value2 == 0) {
                return o1.building.compareTo(o2.building);
            } else {
                return value2;
        }
        return value1;
    }
}


Wednesday, January 15, 2014

Tell me about Java 5 changes

Q) Tell me about Java 5 Var args changes.


A)

In C we can call printf() method with multiple arguments.
printf("%s", 50);
printf("%d %s %s", 250, "Hello", "World");
Varargs was added in Java 5 and the syntax includes three dots  (also called ellipses). Following is the syntax of vararg method.
public void testVar(int count, String... vargs) { }
Notice the dots … in above code. That mark the last argument of the method as variable argument. Also the vararg must be the last argument in the method.
Now let us check simple hello world varargs code.
public class HelloWorldVarargs {
 
    public static void main(String args[]) {
        test(215, "India", "Delhi");
        test(147, "United States", "New York", "California");
    }
 
    public static void test(int some, String... args) {
        System.out.print("\n" + some);
        for(String arg: args) {
            System.out.print(", " + arg);
        }
    }
}


Q) Enhanched for loop?
A) The usual way to step through all the elements of an array in order is with a "standard" for loop, for example,
for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}
The so-called enhanced for loop is a simpler way to do this same thing. (The colon in the syntax can be read as "in.")
for (int myValue : myArray) {
    System.out.println(myValue);
}
The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (Collections are not covered in these pages). It can also be used for arrays, as in the above example, but this is not the original purpose.
Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.
Two additional statement types, break and continue, can also control the behavior of enhanced for loops.

Q) Static import?
A) Static imports concurrency utilities in package java.util.concurrent.