Showing posts with label Java8. Show all posts
Showing posts with label Java8. Show all posts

Thursday, 21 July 2016

Java SE 8: Differences between External Iteration and Internal Iteration

Introduction

In this post, we will discuss one of the important concept of Java SE 8 New features. That is Internal Iteration.

Oracle corporation has introduced "Internal Iteration" new feature as part of Java SE 8 release.


It has many advantages over External Iteration.

Differences between External Iteration and Internal Iteration:

S.NO.EXTERNAL ITERATIONINTERNAL ITERATION
1.Available before Java 8 too.It is introduced in Java SE 8
2.Iterating an Aggregated Object elements externally.Iterating an Aggregated Object elements internally (background).
3.Iterate elements by using for-each loop and Iterators like Enumeration, Iterator, ListIterator.Iterate elements by using Java API like “forEach” method.
4.Iterating elements in Sequential and In-Order only.Not required to iterate elements in Sequential order.
5.It follows OOP approach that is Imperative Style.It follows Functional Programming approach that is Declarative Style.
6.It does NOT separate responsibilities properly that is, it defines both “What is to be done” and “How it is to be done”.It defines only “What is to be done”. No need to worry about “How it is to be done”. Java API takes care about “How to do”.
7.Less Readable Code.More Readable code.
For more Java SE 8 tutorials, please go through my previous posts with "Java8" tag name.

Please drop me a comment if you like my post or have any issues.

Thank you for reading my tutorials.

Wednesday, 13 January 2016

Pillars of OOP and FP Programming Models

In this post, we are going to discuss the main Pillars of the following two popular Programming Models:


  • OOP (Object Oriented Programming)
  • FP (Functional Programming)
Four Main Pillars of OOP:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Two Main Pillars of FP:
  1. Pure Functions
  2. Immutability
Please go through my "Compre FP, IP and OOP" tutorial at :


http://www.journaldev.com/8693/functional-imperative-object-oriented-programming-comparison


That’s it all about Pillars of FP and OOP. We will discuss some more FP concepts in my coming posts.


Please drop me a comment if you like my post or have any issues/suggestions.



Tuesday, 5 January 2016

Java SE8 Interview Questions

In this post, we are going to discuss about Java SE 8 Interview Questions.

As most of the projects are using Java 8 or playing to upgrade to Java 8, most of the interviewers are asking Java 8 Questions.
Java 8 Interview Questions:a) Java SE 8 Interview Questions and Answers (Part-1)  

http://www.journaldev.com/8697/javase8-interview-questions-part1


http://www.journaldev.com/10081/javase8-interview-questions-part2



[ Part - 3 Coming soon..]

That’s it all about Java 8 Interview Questions. We will discuss some more Java SE 8 Interview Questions in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. 

Wednesday, 5 August 2015

Compare Functional Programming Imperativ Programming and OOP


In this post, we will compare the most popular programming paradigms

  1. Functional Programming (FP)
  2. Imperative Programming (IP)
  3. Object Oriented Programming (OOP)

I write tutorials on www.journaldev.com website. 

Click on the following link to get full information.



Please drop me your comments or suggestions to improve my posts.

Thursday, 11 December 2014

Java 8: Iterator Vs Spliterator

Introduction

Spliterator stands for Splitable Iterator. It is newly introduced by Oracle Corporation as part Java SE 8.  Like Iterator and ListIterator, It is also one of the Iterator interface.

S.No. Spliterator Iterator
1. It is introduced in Java SE 8. It is available since Java 1.2.
2. Splitable Iterator Non-Splitable Iterator
3. It is used for both Collection and Stream APIs. It is used for Collection API.
4. It uses Internal Iteration concept to iterate Streams. It uses External Iteration concept to iterate Collections.
5. We can use Spliterator to iterate Streams in Parallel and Sequential order. We can use Spliterator to iterate Collections only in Sequential order.
6. We can get Spliterator by calling spliterator() method on Stream Object. We can get Iterator by calling iterator() method on Collection Object.
7. Important Method: tryAdvance() Important Methods: next(), hasNext()

That’s it about Java 8 Spliterator.

If you want to learn more Java SE Interview Questions, Please access my previous posts:

http://www.journaldev.com/8697/javase8-interview-questions-part1
http://www.journaldev.com/10081/javase8-interview-questions-part2

Please drop me a comment if you like my post or have any issues/suggestions.

Monday, 10 November 2014

JAVA 8: Collection API Vs Stream API


Explain Differences between Collection API and Stream API?

S.No. Collection API Stream API
1. It's available since Java 1.2 It is introduced in Java SE8
2. It is used to store Data(A set of Objects). It is used to compute data(Computation on a set of Objects).
3. We can use both Spliterator and Iterator to iterate elements. We can use both Spliterator and Iterator to iterate elements.
4. It is used to store limited number of Elements. It is used to store either Limited or Infinite Number of Elements.
5. Typically, it uses Internal Iteration concept to iterate Elements. It uses External Iteration to iterate Elements.
6. Collection Object is constructed Eagerly. Stream Object is constructed Lazily.
7. We add elements to Collection object only after it is computed completely. We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand.
8. We can iterate and consume elements from a Collection Object at any number of times. We can iterate and consume elements from a Stream Object only once.

Tuesday, 30 September 2014

Java EE 8 Features

Introduction

In this post, I'm going to list out Java EE (Enterprise Edition) new features. We will discuss these concepts one by one in my coming posts.

Java Enterprise Edition 8 Features

  1. Servlet 4.0
  2. JAX-RS 2.1
  3. JSF 2.3
  4. JMS 2.1
  5. CDI 2.0
  6. JSON-B(JSON Binding) 1.0
  7. MVC 1.0
  8. JCache 1.0

Refer this URL

https://blogs.oracle.com/theaquarium/entry/java_ee_8_roadmap_update

Thank you.

Friday, 6 June 2014

Java 8 : Fibonacci Series

Three techniques to implement Fibonacci Series in Java:

  1. Fibonacci Series With Iteration Approach
  2. Fibonacci Series With Recursion Approach
  3. Fibonacci Series With Java 8 Collectors Approach

Fibonacci Series With Iteration Approach Example:

import java.util.Scanner;

public class FibonacciWithIteration
{

public static void main(String[] args)
  {

System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");

int num = new Scanner(System.in).nextInt();      

       System.out.println("Fibonacci Series With Iteration Approach Upto " + num +" numbers = ");
        for(int i=1; i <= num; i++)
       {
            System.out.print(fibonacciWithIteration(i) +" ");
        }
}
   public static int fibonacciWithIteration(int num)
    {
        if(num == 1 || num == 2){
            return 1;
        }
        int fibo1=1, fibo2=1, fibonacci=1;
        for(int i= 3; i<= num; i++)
        {
            fibonacci = fibo1 + fibo2; 
            fibo1 = fibo2;
            fibo2 = fibonacci;          
        }
        return fibonacci;      
    }   
}

Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Iteration Approach Upto 4 numbers = 
1 1 2 3 



Fibonacci Series With Recursion Approach Example:

import java.util.Scanner;
public class FibonacciWithRecursion
{
public static void main(String[] args)
    {
System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");
int num = new Scanner(System.in).nextInt();      
        System.out.println("Fibonacci Series With Recursion Approach Upto " + num +" numbers = ");
        for(int i=1; i <= num; i++)
        {
            System.out.print(fibonacciWithRecursion(i) +" ");
        }
}
    public static int fibonacciWithRecursion(int num)
    {
    if(num == 1 || num == 2)
        {
            return 1;
        }
      
        return fibonacciWithRecursion(num-1) + fibonacciWithRecursion(num -2);  
    }   
}


Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Iteration Approach Upto 4 numbers = 
1 1 2 3 

Fibonacci Series With Java 8 Collectors Approach Example:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class FibonacciWithCollectors
{
    private final Map<Integer,Long> cache;
    public FibonacciWithCollectors()
   {
        cache = new HashMap<>();
        cache.put(0, 0L);
        cache.put(1, 1L);
    }
    public long fibonacci(int x)
    {
        return cache.computeIfAbsent(x, n -> fibonacci(n-1) + fibonacci(n-2));
    }
    public static void main(String[] args)
    {
    System.out.println("Enter A Number Upto Which Fibonacci Series You Want = ");
int num = new Scanner(System.in).nextInt();      
        System.out.println("Fibonacci Series With Collectors Approach Upto " + num +" numbers = ");
        FibonacciWithCollectors f = new FibonacciWithCollectors();
        for(int i=1; i <= num; i++)
        {
            System.out.print(f.fibonacci(i) +" ");
        }
}
}

Output:

Enter A Number Upto Which Fibonacci Series You Want = 
4
Fibonacci Series With Collectors Approach Upto 4 numbers = 
1 1 2 3 










Tuesday, 27 May 2014

JAVA SE 8 Features

JAVA 8 Features List:

  1. Virtual  Extension Methods: Interface Default Methods 
  2. Virtual  Extension Methods: Interface Static Methods
  3. "Class Wins" Rule
  4. Functional Interface, @FunctionalInterface
  5. Enhanced Type Inference
  6. Lambda Expressions
  7. Functional Interfaces - Function,Consumer,Supplier,Predicate etc
  8. Method References
  9. Constructor References
  10. Stream API
  11. Stream Primitive Specilizations
  12. Date & Time API
  13. Collectors
  14. StringJoiner
  15. Collections API Enhancements
  16. Concurrency Utils Enhancements
  17. java.util.concurrent.atomic Enhancements
  18. Collections Bulk Data Operations
  19. StampedLock
  20. Fork/Join Framework Enhancements
  21. Internal Iteration
  22. Parallel Array Operations
  23. Parallel Collection Operations
  24. Parallel Stream Operations
  25. Arrays Spliterators
  26. Collections Spliterators
  27. Streams Spliterators
  28. Optional
  29. Type Annotations
  30. Method Parameter Reflection
  31. Repeating Annotations
  32. Base64 Encoding and Decoding
  33. IO and NIO2 Enhancements 
  34. Nashorn JavaScript Engine
  35. javac Enhancements
  36. JVM Changes
  37. Java 8 Compact Profiles: compact1,compact2,compact3
  38. JDBC 4.2
  39. JAXP 1.6
  40. Java DB 10.10
  41. Networking
  42. Java Security Changes
Now we'll discuss each topic in detail.

Optional:

  • Optional is a final Class in java.util package
  • It is an alternative to "null" value
  • It is used to avoid "NullPointerException"
Example-1:

package com.way2it.java8.optional;
import java.util.Optional;

public class OptionalTest {
public static void main(String[] args) {
    Optional<String> optional = Optional.of("Rams");
        System.out.println("optional.isPresent() : " + optional.isPresent()); 
        System.out.println("optional.get() : " + optional.get());             
        System.out.println("optional.orElse(\"Posa\") : " + optional.orElse("Posa"));    
        optional.ifPresent((s) -> System.out.println(s.charAt(0)));   
}

Output:

optional.isPresent() : true
optional.get() : Rams
optional.orElse("Posa") : Rams
R

Example-2:-

package com.way2it.java8.optional;
import java.util.Optional;
public class OptionalTest2 {
public static void main(String[] args) {
        Optional<String> optional = Optional.empty();
        System.out.println(optional);
        System.out.println(optional.isPresent());
     
        Optional<String> optional2 = Optional.ofNullable(null);
        System.out.println(optional2);
        System.out.println(optional2.isPresent());
     
        String value = "Rams";
        Optional<String> optional3 = Optional.ofNullable(value);
        System.out.println(optional3);
        System.out.println(optional3.isPresent());
    }
}

Output:

Optional.empty
false
Optional.empty
false
Optional[Rams]
true