|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

 

Recent Questions

  • How to retrieve a key and Value from a map ? view answer
  • Why can an interface supertype call methods that belong to Object class? view answer
  • Q. What is difference between shallow copy and deep copy ? view answer
  • Q.Why to override equals() and hashCode()? and How i can implement both equals() and hashCode() for Set ? view answer
  • Q.How to sort list of objects ( User Defined) using comparator? in Descending Order. view answer
  • Q. How to sort list of objects ( User Defined) using comparator? view answer
  • What is the difference between int and Interger? view answer
  • Singleton Double-checked locking in Java? view answer
  • What are the parameters to follow Creating and Destroying Objects in Java? view answer
  • Q.What are the different scopes for Java variables? view answer
  • Q.What method must be implemented by all threads? view answer
  • Q.Can an unreachable object become reachable again? view answer
  • Q.What are the steps in the JDBC connection? view answer
  • Q.How are this() and super() used with constructors? view answer
  • Q.What is the difference between static and non-static variables? view answer
  • Q.What is the purpose of finalization? view answer
  • Q.Does garbage collection guarantee that a program will not run out of memory? view answer
  • Q.What is synchronization and why is it important? view answer
  • Q.What is daemon thread and which method is used to create the daemon thread? view answer
  • Q.What are synchronized methods and synchronized statements? view answer
  • Q.If I write System.exit (0); at the end of the try block, will the finally block still execute? view answer
  • Q.If I write return at the end of the try block, will the finally block still execute? view answer
  • Q.Is it necessary that each try block must be followed by a catch block? view answer
  • Q.What is the basic difference between the 2 approaches to exception handling. 1> try catch block and 2> specifying the candidate exceptions in the throws clause? When should you use which approach? view answer
  • Q.What are the different ways to handle exceptions? view answer
  • Q.What is the difference between error and an exception? view answer
  • Q: What is wrapper class? Explain with example? view answer
  • Q.What is serialization? Explain with example? view answer
  • Q.What one should take care of while serializing the object? view answer
  • Q. When you serialize an object, what happens to the object references included in the object? view answer
  • Q.What happens to the static fields of a class during serialization? view answer
  • Q. Objects are passed by value or by reference? view answer
  • Q. Primitive data types are passed by reference or pass by value? view answer
  • Q. What type of parameter passing does Java support? view answer
  • Q.Can a top level class be private or protected? view answer
  • Q.What is the default value of an object reference declared as an instance variable? view answer
  • Q.What is the difference between declaring a variable and defining a variable? view answer
  • Q. What are different types of inner classes? view answer
  • Q. What is Overriding? view answer
  • Q.What are Checked and UnChecked Exception? view answer
  • Q. What is final? view answer
  • Q.What is static in java? view answer
  • What is an abstract class? view answer
  • Q. What are the modifiers in Java ? view answer
  • Q.Difference between HashMap and HashTable? view answer
  • Q.Difference between Vector and ArrayList? view answer
  • Q.Difference between ArrayList and LinkedList? view answer
  • Q.What are pass by reference and passby value in Java? view answer
  • Q. When we go for Abstract and Interface in Java? view answer
  • Q.What is the difference between interface and abstract class? view answer
  • Q.What are the Garbage collection algorithms in Java? view answer
  • Q.What is garbage collection and the purpose of garbage collection in Java? view answer
  • Q. What is the difference between an Interface and an Abstract class? view answer

!!! Java Frequently Asked Questions !!!

Q. How to sort list of objects ( User Defined) using comparator?

SCJP 1.5/1.6 Exam Kit

!!!Answer!!!- From Technical Expert

You can use Collections.sort(List,Comparator) to sort objects.
Example : You have User Bean , you want to sort based on username filed.
User Class :
public class User {
String userName = "";
String city = "";
String state = "";
/**     * @return Returns the userName.
     */
    public String getUserName() {
        return userName;
    }
    /**
     * @param userName The userName to set.
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Then you have to write a comparator class.
public class UserNameComparator implements Comparator {
     public int compare(Object user, Object anotherUser) {
    
     String firstName1 = ((User) user).getUserName().toUpperCase();
    
     String firstName2 = ((User) anotherUser).getUserName().toUpperCase();
    
     return firstName1.compareTo(firstName2);
    
     }
    }

Now sorting code
User user1 = new User();
        user1.setUserName("das");
        User user2 = new User();
        user2.setUserName("nick");
        User user3 = new User();
        user3.setUserName("ram");
        User user4 = new User();
        user4.setUserName("jadu");
        
        ArrayList list = new ArrayList();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        
        Collections.sort(list,new UserNameComparator()); // sort ascending order.
        
        for(int i=0;i<list.size();i++){
            User usr = (User)list.get(i);
            System.out.println(usr.getUserName());
        }

Answered By : null Replied Date : Mar 28 2012
Answer :


Answered By : null Replied Date : Mar 26 2012
Answer :


Answered By : null Replied Date : Mar 11 2012
Answer :


Answered By : null Replied Date : Feb 21 2012
Answer :


Answered By : null Replied Date : Feb 20 2012
Answer :


Answered By : null Replied Date : Feb 17 2012
Answer :


Answered By : null Replied Date : Feb 2 2012
Answer :


Answered By : null Replied Date : Jan 26 2012
Answer :


Answered By : null Replied Date : Jan 13 2012
Answer :


Answered By : null Replied Date : Jan 7 2012
Answer :


Answered By : null Replied Date : Jan 7 2012
Answer :


Answered By : null Replied Date : Dec 21 2011
Answer :


Answered By : null Replied Date : Dec 13 2011
Answer :


Answered By : null Replied Date : Nov 29 2011
Answer :


Answered By : null Replied Date : Nov 25 2011
Answer :


Answered By : null Replied Date : Nov 22 2011
Answer :


Answered By : null Replied Date : Nov 9 2011
Answer :


Answered By : null Replied Date : Nov 5 2011
Answer :


Answered By : null Replied Date : Oct 25 2011
Answer :


Answered By : null Replied Date : Oct 19 2011
Answer :


Answered By : null Replied Date : Oct 17 2011
Answer :


Answered By : null Replied Date : Oct 13 2011
Answer :


Answered By : null Replied Date : Oct 4 2011
Answer :


Answered By : null Replied Date : Oct 3 2011
Answer :


Answered By : null Replied Date : Sep 23 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 19 2011
Answer :


Answered By : null Replied Date : Sep 9 2011
Answer :


Answered By : null Replied Date : Sep 5 2011
Answer :


Answered By : null Replied Date : Aug 28 2011
Answer :


Answered By : null Replied Date : Aug 24 2011
Answer :


Answered By : null Replied Date : Aug 22 2011
Answer :


Answered By : null Replied Date : Aug 22 2011
Answer :


Answered By : null Replied Date : Aug 21 2011
Answer :


Answered By : null Replied Date : Aug 14 2011
Answer :


Answered By : null Replied Date : Aug 14 2011
Answer :


Answered By : null Replied Date : Jul 29 2011
Answer :


Answered By : null Replied Date : Jun 27 2011
Answer :


Answered By : null Replied Date : Jun 27 2011
Answer :


Answered By : null Replied Date : Jun 3 2011
Answer :


Answered By : null Replied Date : May 10 2011
Answer :


Answered By : null Replied Date : Apr 18 2011
Answer :


Answered By : null Replied Date : Apr 14 2011
Answer :


Answered By : null Replied Date : Apr 11 2011
Answer :


Answered By : null Replied Date : Apr 8 2011
Answer :


Answered By : null Replied Date : Apr 2 2011
Answer :


Answered By : null Replied Date : Mar 31 2011
Answer :


Answered By : null Replied Date : Mar 30 2011
Answer :


Answered By : null Replied Date : Mar 29 2011
Answer :


Answered By : null Replied Date : Mar 28 2011
Answer :


Answered By : null Replied Date : Mar 25 2011
Answer :


Answered By : null Replied Date : Mar 25 2011
Answer :


Answered By : null Replied Date : Mar 24 2011
Answer :


Answered By : null Replied Date : Mar 21 2011
Answer :


Answered By : null Replied Date : Mar 21 2011
Answer :


Answered By : null Replied Date : Mar 21 2011
Answer :


Answered By : null Replied Date : Mar 19 2011
Answer :


Answered By : null Replied Date : Mar 17 2011
Answer :


Answered By : null Replied Date : Mar 14 2011
Answer :


Answered By : null Replied Date : Mar 13 2011
Answer :


Answered By : null Replied Date : Mar 10 2011
Answer :


Answered By : null Replied Date : Mar 10 2011
Answer :


Answered By : null Replied Date : Mar 9 2011
Answer :


Answered By : null Replied Date : Mar 9 2011
Answer :


Answered By : null Replied Date : Mar 5 2011
Answer :


Answered By : null Replied Date : Feb 26 2011
Answer :


Answered By : null Replied Date : Feb 16 2011
Answer :


Answered By : null Replied Date : Feb 14 2011
Answer :


Answered By : null Replied Date : Jan 22 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Dec 27 2010
Answer :


Answered By : null Replied Date : Dec 12 2010
Answer :


Answered By : null Replied Date : Nov 20 2010
Answer :


Answered By : null Replied Date : Oct 29 2010
Answer :


Answered By : null Replied Date : Oct 10 2010
Answer :


Answered By : null Replied Date : Oct 2 2010
Answer :


Answered By : null Replied Date : Sep 28 2010
Answer :


Answered By : null Replied Date : Sep 17 2010
Answer :


Answered By : null Replied Date : Sep 16 2010
Answer :


Answered By : null Replied Date : Sep 14 2010
Answer :


Answered By : null Replied Date : Sep 12 2010
Answer :


Answered By : null Replied Date : Sep 9 2010
Answer :


Answered By : null Replied Date : Sep 8 2010
Answer :


Answered By : null Replied Date : Sep 3 2010
Answer :


Answered By : null Replied Date : Aug 9 2010
Answer :


Answered By : null Replied Date : Aug 9 2010
Answer :


Answered By : suresh Replied Date : May 16 2012
Answer : hai


Answered By : null Replied Date : May 21 2012
Answer :


Answered By : null Replied Date : Jun 24 2012
Answer :


Answered By : null Replied Date : Jun 26 2012
Answer :


Answered By : null Replied Date : Jul 12 2012
Answer :


Answered By : null Replied Date : Jul 30 2012
Answer :


Answered By : null Replied Date : Aug 6 2012
Answer :


Answered By : null Replied Date : Aug 13 2012
Answer :


Answered By : null Replied Date : Aug 18 2012
Answer :


Answered By : null Replied Date : Aug 19 2012
Answer :


Answered By : null Replied Date : Aug 25 2012
Answer :


Answered By : null Replied Date : Sep 8 2012
Answer :


Answered By : null Replied Date : Sep 8 2012
Answer :


Answered By : null Replied Date : Sep 11 2012
Answer :


Answered By : null Replied Date : Oct 10 2012
Answer :


Answered By : null Replied Date : Oct 20 2012
Answer :


Answered By : null Replied Date : Nov 6 2012
Answer :


Answered By : null Replied Date : Nov 10 2012
Answer :


Answered By : null Replied Date : Nov 20 2012
Answer :


Answered By : null Replied Date : Dec 30 2012
Answer :


Answered By : null Replied Date : Dec 30 2012
Answer :


Answered By : null Replied Date : Dec 30 2012
Answer :


Answered By : null Replied Date : Jan 3 2013
Answer :


Answered By : null Replied Date : Jan 9 2013
Answer :


Answered By : null Replied Date : Jan 22 2013
Answer :


Answered By : null Replied Date : Mar 10 2013
Answer :


Answered By : null Replied Date : Mar 14 2013
Answer :


Answered By : null Replied Date : Apr 15 2013
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

The information you are posting should be related to java and ORACLE technology. Not political.