Java 8 Comparator Example Using Lambda Expressions

In short, You can implement Comparator using lambda expression because it is a SAM type interface. It just got one abstract method compare() which means you can pass lambda expression where a Comparator is expected. Many Java programmer often ask me, what is the best way to learn lambda expression of Java 8? Of course by using it on your day to day programming task. Since implementing equals(), hashcode(), compareTo(), and compare() methods are some of the most common tasks of a Java developer, it makes sense to learn how to use the lambda expression to implement Comparable and custom Comparator in Java. One question comes to mind, can we use lambda expression with Comparator? because it's an old interface and may not implement functional interface annotated with @FunctionalInterface annotation?

Difference between jsp:include and jsp:forward action- Example

JSP provides standard actions to do things without using Java inside the scriptlet. Two of such standard actions, which help JSP to interact with other server resources e.g. another JSP, Servlet or HTML files are, include and forward actions. The <jsp:forward> action enables you to forward an HTTP request to a static HTML file, a servlet, or another JSP. It has an attribute called page, which accepts the URL of another resource as shown below:

<jsp:forward page="URL" /> 

How to sort a LinkedList in Java? Example Tutorial

Since LinkedList implements the java.util.List interface, you can sort the LinkedList by using Collections.sort() method, just like you sort an ArrayList. Since LinkedList class implements the linked list data structure which doesn't provide random access based upon the index, sorting is quite expensive. In order to access any element, you need to first traverse through that element which is O(n) operator. This method uses an efficient strategy to handle this scenario. It first copies the contents of LinkedList to an array, sorts the array and copies it back. So it's as efficient as sorting an ArrayList. By default Collections.sort() arrange elements of linked list into their natural order of sorting but it also accepts a Comparator, which can be used to sort elements in custom order. Java 8 also introduced a new sort() method on the java.util.List interface itself, which means you no longer need Collections.sort() to sort a LinkedList, you can do directly by calling the LinkedList.sort() method in Java 8. See Java SE 8 for Really Impatient to learn more about new features of Java 8. In this article, I'll show you a couple of examples of sorting LinkedList in Java.

5 Books to Improve Coding Skills of Programmers - Must Read, Best of Lot

Learning a Programming language e.g. Java or C++ is easy but learning to write good code is not. Writing good code is an art and also an important differentiating factor between an average programmer vs a good programmer. Since most of the programmers often look for inspiration and resource to improve their coding skill, I decide to share some of the good books which can help them to improve their coding. Since many universities, colleges and training courses only teach programming languages but not the art of coding, it still remains one of the self-learned skill. The internet has helped a lot to coders with several websites coming up to teach code, programming contest, helping to solve you programming interview questions and all, but IMHO, books are still vital for overall improvement. In this article, I am going to share some of the great books, written by both great author and great programmers, which can certainly help you to write good code and become a better programmer.

java.lang.IllegalStateException: getWriter() has already been called for this response

This error comes when a Servlet calls the getOutputStream() method on response object for writing something after calling the include() method. Suppose a Servlet calls the include() method to load the response of a JSP. Since JSP has already written the response on it hence again opening OutputStream on response object is illegal, you get the java.lang.IllegalStateException: getWriter() has already been called for this response error. This error also comes when you try to include response of another Servlet and than tries to write something on output stream again. In short, your Servlet should never write anything on response object after calling the include() method. 

15 Technical Core Java Interview Questions Answers for Experienced Developers

When the experience of a Java Programmer grows in the years e.g. when it goes from beginner years ( 2 to 4) to more experience or sort of senior level ( 5 to 7 years), Core Java Interview Questions also changes a bit. Of course, basics like data structure, algorithms, and object-oriented programming remains same, but types of questions will become more advanced and their answers will definitely need to be more detailed and accurate. I often receive queries about core Java questions asked to a senior developer of 5 to 6-year experience, or, sometimes, I am going for an interview of senior Java developer, what kind of questions I should expect. This sometimes puzzles me, that once you become senior, you automatically starts taking part in the interview, and you should have an idea of what to expect on Interviews, but at the same time, I can understand that having an idea of questions before going on Interview, helps preparation. Of course, you are not going to get a question like the one you have faced on 2 to 3 years level Java Interviews, but It also depends on different rounds of Interviews.

How to get Current Date and Time in Java with Example

You can get today's date or current date and time in Java by creating a new Date() object. A java.uti.Date object represents the current date and time instance in Java. Now, if you want to date in DDMMYYYY format or any other date format, you can easily convert the Date object to String by using SimpleDateFormat class. Since Date instance contains both date and time, if you only need a date then you can further convert the java.util.Date to java.sql.Date. Similarly, if you need just time then you can convert the java.util.Date to java.sql.Time, they represent the date and time part of java.util.Date object in Java. You can also use the java.util.Calendar class to get the today's date and time. Further, if you are using Java 8 then you can use new Date and Time API classes like LocalDate, LocalTime, and LocalDateTime to get the current date, time, and date-time in Java.

Can You Declare Constructor inside Servlet Class?

Yes, Servlet can have Constructor, it's perfectly legal but it's not the right way to initialize your Servlet. You should use the init() method provided by the Servlet interface to initialize the Servlet. If you remember, Servlet's are special in a sense that they are instantiated by the container and managed by the container. A servlet container like Tomcat creates a pool of multiple Servlets to serve multiple clients at the same time. They instantiate Servlet by calling the default no-argument constructor and suppose you have declared another constructor which takes a parameter e.g. HelloServlet(String name) than Java compiler will not add the default no-argument constructor and Servlet container will not able to initialize the Servlet. That's why it's important not to provide a constructor in Servlet, but if you do, make sure you also add a default constructor there for Servlet container.

Difference between HashSet and TreeSet in Java

Difference between HashSet and TreeSet in Java
There are several differences between an HashSet and a TreeSet is similar to what we discussed as a difference between TreeMap and HashMap. Anyway, Set and Map are two completely different interfaces so we will revisit those differences here. Probably the most important difference between HashSet and TreeSet is the performance. HashSet is faster than TreeSet which means if you need performance use HashSet but HashSet doesn't provide any kind of ordering so if you need ordering then you need to switch to TreeSet which provides sorting of keys. Sorting can be natural order defined by a Comparable interface or any particular order defined by Comparator interface in Java.

How to Increase Heap Size of Java Program running in Eclipse

It is possible to increase the heap size allocated by the JVM by using command line options Here we have 3 options

-Xms<size>        to set the initial Java heap size
-Xmx<size>        to set the maximum Java heap size
-Xss<size>          to set the java thread stack size

$ java -Xms1G -Xmx2G MainClass

In the above line, we have set the minimum heap size to 1GB and maximum heap size to 2GB.

How to convert Vector to array in Java? 2 Examples

In the last article, we have learned how to convert a LinkedList to array in Java and in today's tutorial you will learn how to convert a Vector to array in Java. There is a similarity between Vector and LinkedList, both implements the List interface. Even though Vector is a legacy class and exists in Java from JDK 1.1 long before Java Collection framework was introduced, it was later retrofitted to implement the List interface. You can use the same toArray() method to convert a Vector to an array in Java. Similar to LinkedList, you can convert Vector of any object to an array of the corresponding object, the only restriction is that you cannot convert a vector of Integers to an array of ints or Vector of Float objects to an array of floats, which applies to our linked list solution as well.

Top 20 Hibernate Interview Questions for Java J2EE Programmers

Hibernate is one of the most popular persistent frameworks in Java world. Hibernate offers object to relational (ORM) solution which frees Java developers from writing tedious, hard to read and cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developer from writing JDBC and database interaction code, Hibernate also offers the out-of-box solution on caching, proxying and lazy loading which drastically improves the performance of your Java Web application. Given it's important in Java Web application development field Hibernate has become of one of the  most sought after skill and goes hand in hand with Spring framework. That's why Hibernate interview questions are also very popular in Java interviews. Earlier I have shared some Spring MVC interview questions and due to popular demand, I am now sharing 20 odd Hibernate questions from various Java interviews.

How to convert LinkedList to array in Java? Example

You can convert a LinkedList to an array in Java by using the toArray() method of LinkedList. The toArray() method accepts an array of relevant type to store contents of LinkedList. It stores the elements in the same order they are currently inside LinkedList. By using the toArray() method you can convert any type of LinkedList e.g. Integer, String or Float to any type of Array, only catch is this you cannot convert a LinkedList to an array of primitives i.e. a LinkedList of Integer cannot be converted into an array of ints by using using toArray() method, but you can convert it to array of Integer objects, that's perfectly ok. Similarly, you can convert LinkedList of Double to an array of Double and LinkedList of Float objects to an array of floats.

What is Inheritance in Java with example - Object Oriented Programming Tutorial

What is Inheritance in Java
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows coding reusability.  In other words, Inheritance  self-implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation and Polymorphism, Inheritance forms the backbone of Object oriented programming and Java.  In Java, we use the term inheritance when one object acquires some property from other objects. In Java, inheritance is defined in terms of superclass and subclass. it is normally used when some object wants to use existing feature of some class and also want to provide some special feature, so we can say inheritance has given the advantage of reusability.

How to loop over TreeSet in Java with example

In our earlier articles, we have learned how to loop over ArrayList in Java and you can use the same logic to loop over a TreeSet. You can use both, enhanced for loop and Iterator to traverse over TreeSet in Java. Though, worth noting is that Iterator returned by the iterator() method of TreeSet returns elements in the sorted order which is imposed by the Comparator you have provided to TreeSet at the time of instantiation. By default, if you don't provide any custom Comparator than TreeSet sorts elements in their natural order e.g. String elements are sorted in alphabetic order and Integer elements are sorted in numeric order. When you iterate over a TreeSet the iterator follows this order.