Powered By Blogger

Tuesday, August 30, 2011

Comparable Interface : Problem while adding object to TreeSet

public class X{
  public static void main(String [] arg){
     Set s=new TreeSet();
     s.add(new Person(20));
     s.add(new Person(10));
     System.out.println(s);
 }
}
class Person{
    int i;
    Person(int i){
         i = this.i;
    }
}

Can anyone tell me why this code fragment shows me the “ClassCastException”, Using Java 2 1.5 compiler.
In the above code
Set s=new TreeSet();
Set S which is a TreeSet accepts an object that is very true when developer adds an object new Person(20) to code, code will not fail. Now when developer will add another object new Person(10) the code will fail.
Now replace the code with a fresh code
Set s=new TreeSet();
s.add(new Integer(10)); // replace person object with Integer object
s.add(new Integer(20)); // replace person object with Integer object
System.out.println(s);

The above code will not fail.
Now lets dig in to the concept.
TreeSet is a class belongs to Set interface, being a set it does not accept duplicates. First object insertion goes fine here,Now when we are trying to add the second object then there must be a criteria based on which the 2 object should be comparable. And in the above code there is no such criteria, for uniqueness the set internally use compareable interface to check object equality which is not implemented by the Person class,So in this case developer have to implement the comparable interface in his Person Class.
There are fourteen classes in the Java 2 SDK, version 1.2, that implements the Comparable interface.
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short, Character, CollationKey, Date, File, ObjectStreamField, String
So when developer will try t add the above objects they won’t give any problem, as developer is facing in above scenario.
Below code for person class will solve the developer’s problem.
public class Person implements Comparable{
    int i;
    Person(int i){
        i = this.i;
    }

 public int compareTo(Object o1) {
        if (this.i == ((Person) o1).i)
               return 0;
        else if ((this.i) > ((Person) o1).i)
            return 1;
        else
            return -1;

    }
}

Tuesday, August 9, 2011

What is the difference between mvc1 and mvc2?


In MVC-1 architecture (also referred as Model 1), the request is first handled by a JSP, that interacts with a Bean. Here the JSP page may have partial processing logic, although a bulk of processing logic may be handled by the beans that may interact with the database. The JSP in this case in addition to being responsible for 'View' of MVC also takes the resposibility as 'Controller', and the beans acting as Model. For small applications that do not have complex processing, this model may be fine, but in case of bigger applications where a lot of processing and decision making is required (Authentication, Logging, Conditional redirection, database interactions, network connections) this is not the best option.
In such cases MVC2 or Model 2 architecture is the better option. It has a Controller Servlet that handles all the incoming requests (You may refer to the Front Controller pattern) and acts as the 'Controller', it determines what comes next, a View (JSP) or further processing by Model (Beans doing all the complex tasks), and will decide the view to display the results from the Model.
The links on the JSP pages for next view may also pass through the controller servlet to determine the next view, unlike in MVC-1 where the links on a JSP page will directly point to another JSP page.