Thursday, April 9, 2015

Java 8 without Lambdas to print elements using short hand

Goal
Print all elements in List with Java 8 without Lambdas and a short hand input.

Setup
In following code lets fill in the blanks to print all elements of given list.

@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(/* fill the code here */);

    }

Solution
As the solution is small and trivial the output looks as follows.


@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(System.out::println);

    }

Java 8 with lambda expression to print elements on screen

Goal
Using Java 8 and its new feature Lambdas lets print each element of List to screen.

Setup
We will start with following code and fill in to a piece of logic to push elements on console.

@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(/* fill with lambda expression */);

    }

Goal
We will a lambda expression e -> {/** code block **/} to manage the output logic. Something like this
value -> {
            System.out.println(value);
        }

The complete solution looks like following

@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(value -> {
            System.out.println(value);
        });

    }

Java 8 For Each with Consumer example

Goal
Java 8 comes with new features which adds forEach and Consumer to its list. Lets write a simple piece of code that uses forEach and Consumer<> to print the elements on screen.

Setup
Following code is given to us and need to fill in the blank to print the elements.

@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(/** fill with Consumer and print to screen.**/
);

    }

Solution
We can use follwing code to fill in the blank. This code uses Consumer and implements the method accept(). In accept() we can write any piece of logic. Here we will simply print elements on screen.

new Consumer() {
            public void accept(Integer value) {
                System.out.println(value);
            }

The complete program looks like as following :

@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6);

        values.forEach(new Consumer() {
            public void accept(Integer value) {
                System.out.println(value);
            }
        });

    }

Sort Collection using sort function and Java 8

Goal
Sorting is widely used in daily routine to modify data arrangement. In this blog the input is a list of alphabets. Using Java 8 and new sort method. This blog introduces List.sort() as a small example.

Setup
Lets start with following code. As shown it prints unsorted array elements.

         @Test
    public void testCollectionSorted() {
        List list = Arrays.asList("b", "f", "e", "a");
        
        // print each elements of list object
        list.forEach(input -> System.out.println(input));
    }

Solution
Now we will use List.sort() and pass a lambda expression which will sort the list

list.sort((e1e2) -> {
            return e1.compareTo(e2);
        });

Thats it. Here is the full method which can be used to sort and print array contains alphabets.

         @Test
    public void testCollectionSorted() {
        
        List list = Arrays.asList("b", "f", "e", "a");
        list.sort((e1, e2) -> {
            return e1.compareTo(e2);
        });

        list.forEach(input -> System.out.println(input));
}

Tuesday, April 7, 2015

Hello World in Java

In this blogpost I will show you how to write SIMPLE hello world program in Java using command line.

On Windows :
> Type cmd in start
> in cmd type notepad press enter
> In notepad type following



> Save the notepad to cmd's present working directory
> execute javac "filename.java"
> execute java -classpath . filename.class
> Output should be "Hello World!"

Have fun .. this is just a sample blog......