Monday, February 22, 2016

Stream Introduction Part 1.

Stream Introduction.

Java 8 came with lot of new things. Few buzz words you might have heard can be Lambdas, closures, JavaScript Nashorn, Streams, and others. Yes, there are lot of exciting concepts introduced in Java 8. In this blog post I am going to discuss about Stream.

This is first article regarding Streams so please be patient.
Let us first take a use case and learn Streams from it.

Suppose I have a Person class with name and age as constructor parameters. Below is the code for Person class.

 * Currently I do not have any plans to extend any class so
 * making this class final.
 * */
public final class Person {

      private final String name;
      private final int age;

      public Person(final String name, final int age) {
   = name;
            this.age = age;

       * @return name
       * */
      public String name() {
            return name;

       * @return age
       * */
      public int age() {
            return age;

      public String toString() {

            StringBuilder sb = new StringBuilder();
            return sb.toString();

Now you have List<Person> that will hold several Person objects. And now you want to get all Person with age greater than 35 sorted by name.

So how to solve this scenario?

Well, it is simple isn’t it? One way to do this is:

  • Create a new List<Person> who will have age greater than 25.

       List<Person> qualifiedPeople = new ArrayList<Person>();
      for (final Person person : people) {
            if (person.age() > 25) {

  •  Now just sort the new List which we have named as qualifiedPeople

             Collections.sort(qualifiedPeople, new Comparator<Person>() {
            public int compare(final Person person1,
                               final Person person2) {

And that’s it. You have List<Person> whose age is greater than 25 and sorted by name.

The problem with this approach is that we are saying or telling how to solve the problem. Couple of things to note here:

·         Writing Imperative Code(Telling HOW? instead of WHAT?). Read more imperative code here.
·         Doing iteration i.e. writing code that is not a use-case or writing Boiler-plate code.
·         Writing anonymous inner class. Syntax can be confusing sometimes. And whenever you want to sort a List it will create a new Object.

There are only two lines from above that are important.
if (person.age() > 25) and

Wouldn’t it be nice if the code can be reduced?

Let us write this code in Declarative way i.e. we just tell WHAT we want.

List<Person> p
                  .filter(person -> person.age() > 25)
                  .sorted(Comparator.comparing(person ->

By the way, listOfPeople() method would look like this.

      public static List<Person> listOfPeople() {
            List<Person> people = new ArrayList<Person>();
            people.add(new Person("John", 25));
            people.add(new Person("Jane", 37));
            people.add(new Person("Robert", 50));
            people.add(new Person("Sansa", 21));
            people.add(new Person("Arya", 17));
            people.add(new Person("Ned", 54));
            people.add(new Person("Jaime", 35));
            people.add(new Person("Tyrion", 5));
            return people;


In above lines of code much things are going on. Be patient.

·         ->” is a lambda operator. Read What is Functional Interface and Using Functional Interface posts.
·         Comparator has a method comparing. That’s weird right. Comparator is interface. Well comparing is default method in Comparator interface. We will talk about it later.
·         Collector.toList() is a method that converts data from Stream to List.

If you see closely to the code, then you will know that there is no external iteration. No temporary variables. We are telling WHAT to do and not HOW to do.

This is a huge step in Java for Java Developers as we can now write code that will contain less temporary variables or objects.
We will learn more about this in next post. If you find anything wrong or have any doubt please pen it down in comments section.

To read Stream Introduction Part-2 click here.

Sunday, February 21, 2016

Collection Framework Interface Quick Reference Guide.

Java Collections Framework Interface quick reference guide. The below reference guide will act as quick overview of all Collections framework interfaces.



Iterable Interface Quick Reference Guide

Iterable<E> Interface Quick Reference Guide

What is Iterable?
Implementing this interface allows the object to be target of for-each loop. Iterable interface had only one method prior to Java 8. But as of Java 8, it has 2 additional methods. Collection interface extends Iterable interface so all the Collections are Iterable.


Iterator<T> iterator()
Returns the iterator that iterates over elements of type T.

default void
forEach(Consumer<? super T> action)

forEach is default method. This method performs action on every elements of on target Iterable. The argument Consumer is a Functional Interface. Read about it here.

default Spliterator<T> spliterator()

Creates a Spliterator over the elements described by this Iterable.

Java Interview Questions Part 8.

Java Interview Questions Part 8.

1.       What is casting?

2.       What restrictions are placed on the values of each case of a switch statement?

3.       Are arrays primitive data types?

4.       What is an object's lock?

5.       Can a Byte object be cast to a double value?

6.       How HashMap works in Java?

7.       What value does read() return when it has reached the end of a file?

8.       What modifiers may be used with a top-level class?

9.       Are JVM's platform independent?

10.   To what value is a variable of the boolean type automatically initialized?

11.   What is a Constructor Overloading?

12.   What is method overloading?

13.   What is the difference between ArrayList and Vector?

14.   Does Java support multiple inheritance?

15.   What is difference between static method and instance method?

16.   How is rounding performed under integer division?

17.   What are the restriction imposed on a static method or a static block of code?

18.   What is the difference between inner class and nested class?

19.   What is the most important feature of Java?

20.   What is the difference between an if statement and a switch statement?

Java Interview Questions Part 7.

Java Interview Questions Part 7.

1.       What is the difference between method overriding and overloading?

2.       Does a class inherit the constructors of its superclass?

3.       What is abstraction?

4.       What gives Java its 'write once and run anywhere' nature?

5.       Which non-Unicode letter characters may be used as the first character of an identifier?

6.       What is composition?

7.       What is the difference between the prefix and postfix forms of the ++ operator?

8.       What is static variable?

9.       How many types of memory areas are allocated by JVM?

10.   Does Java support multiple inheritance?

11.   Can an Interface extend another Interface?

12.   What is constructor?

13.   Which Java operator is right associative?

14.   Can a method be overloaded based on different return type but same argument type?

15.   What is the range of the short type?

16.   Can a class declared as private be accessed outside it's package?

17.   What is a native method?

18.   What is difference between Comparable and Comparator?

19.   What is the return type of a program's main() method?

20.   Can you give few examples of final classes defined in Java API?

Java Interview Questions Part 6.

Java Interview Questions Part 6.

1.       Is main() method mandatory in all java classes?

2.       Can a source file contain more than one class declaration?

3.       What is difference between aggregation and composition?

4.       Can a class be declared as static?

5.       What modifiers are allowed for methods in an Interface?

6.       Can an Interface be defined inside a class?

7.       What is numeric promotion?

8.       Can you create an object of an abstract class?

9.       What is JIT compiler?

10.   What is Runtime Polymorphism?

11.   What if the static modifier is removed from the signature of the main method?

12.   Can we declare a static variable inside a method?

13.   What is the difference between creating String as new() and literal?

14.   What is a Marker Interface?

15.   What is an Iterator?

16.   Which object oriented Concept is achieved by using overloading and overriding?

17.   When can an object reference be cast to an interface reference?

18.   What restrictions are placed on the location of a package statement within a source code file?

19.   What is the argument of main() method?

20.   Are true and false keywords?

Friday, February 19, 2016

Java Interview Questions Part 5.

Java Interview Questions Part 5.

1.       Can an interface implement another Interface?

2.       Name the eight primitive Java types.

3.       Which two methods you need to implement for key Object in HashMap?

4.       What is the base class of all classes?

5.       What is final variable?

6.       What is the access scope of a protected method?

7.       Which class is extended by all other classes?

8.       What is the difference between a public and a non-public class?

9.       How many static initializers can you have?

10.   What is final method?

11.   Is Java a pure object oriented language?

12.   What is an abstract method?

13.   What modifiers may be used with an inner class that is a member of an outer class?

14.   Which class is the superclass for every class?

15.   What is a pointer and does Java support pointers?

16.   What differences exist between HashMap and Hashtable?

17.   What is the difference between an Interface and an Abstract class?

18.   If a class is declared without any access modifiers, where may the class be accessed?

19.   Why isn't there operator overloading?

20.   What is the importance of hashCode() and equals() methods?

Thursday, February 18, 2016

Java Interview Questions Part 4.

Java Interview Questions Part 4.

1.       Can an Interface be final?

2.       Can we execute a program without main() method?

3.       Can a Class extend more than one Class?

4.       What happens to a static variable that is defined within a method of a class?

5.       What is static method?

6.       Can an anonymous class be declared as implementing an interface and extending a class?

7.       How to avoid inheritance of a class?

8.       Why Collection doesn’t extend Cloneable and Serializable interfaces?

9.       What happens when you add a double value to a String?

10.   What is Classloader?

11.   Can there be any abstract method without abstract class?

12.   What is the difference between abstraction and encapsulation?

13.   How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

14.   What is the Difference between JDK and JRE?

15.   What is Comparable interface?

16.   How to define a constant variable in Java?

17.   What are order of precedence and associativity, and how are they used?

18.   Why does Java not support operator overloading?

19.   What are the Data Types supported by Java?

20.   Write code to find the First non-repeated character in the String?

Ads Inside Post