Category Archives: java 8

Kobalt Lombok Configuration

Prerequisites

These are the pre-requisite of Project Lombok to configure in any applications and IDE.

Java >= 1.8
Gradle >= 4.x or Maven 3.6.x
Project Lombok >= 1.18.20

See Also:

Lombok Build Tool Configuration

To setup Lombok on any of the build tool required on time of compilation of code only. Lombok not required on time of testing, running or deployment of code. So while adding the lombok dependency always add as “provided”.

Kobalt Lombok Configuration

You can add lombok dependency as below for kobalt build tool

dependencies {
	provided("org.projectlombok:lombok:1.18.28")
}

References

Related Posts

Your Feedback Motivate Us

If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

Happy Learning !!!

Eclipse Lombok Configuration

In your Eclipse based application, you can follow the below Eclipse Lombok Configuration to support you IDE for Project Lombok annotations.

Prerequisites

These are the pre-requisite of Project Lombok to configure in any applications and IDE.

Java >= 1.8
Gradle >= 4.x or Maven 3.6.x
Project Lombok >= 1.18.20

Ways to configure Lombok in Eclipse

In this post, you will know about the three ways on configuring the Lombok in you Eclipse IDE.

  • Manually configure the Eclipse
  • Eclipse Plugin for Lombok
  • Eclipse Software Center

Manually Configure Eclipse for Lombok

  • Download the lombok.jar from https://projectlombok.org/download
  • Place this lombok.jar in you root folder of eclipse installation location
  • Double click on the jar, it will open open popup as below. Select the IDE location by click “Specify location”
  • Click on “install/update” button.
Eclipse Lombok Configuration
Eclipse Lombok Configuration 1
  • Click on “Install/Update” button. On successful installation you will get below screen
  • Click on “Quit Installer”
  • Restart you eclipse IDE to refresh the Lombok features in case already open.
    Eclipse Lombok Configuration
    Eclipse Lombok Configuration 2

    Lombok Eclipse Configuration by Plug-in

    You can install lombok directly from within eclipse, and in that way, you can also include lombok as part of your team eclipse deployment configuration.

    Follow below steps to configure the Lombok Eclipse configuration by plug-in

    • Go to your eclipse “Help->Search->Install New Software” as given
    Eclipse Lombok Configuration by plug-in
    Eclipse Lombok Configuration by plug-in
    Eclipse Lombok Configuration by plug-in
    Eclipse Lombok Configuration by plug-in
    • Select the Lombok Plug-in check boxes and click on install button
    • After Installation successful, Restart your eclipse IDE to refresh lombok features.

    Eclipse Lombok from Software Center

    Some of the latest version of Eclipse IDE already supports Lombok Project. In this case you don’t required any of above steps:

    To check Lombok Support in eclipse IDE you can follow these steps:

    • Go to the about dialogue
    • Supported version on Lombok detail will display at last in “Copy Write” section as given in screenshot.
    Eclipse Lombok Configuration
    Eclipse Lombok Configuration

    References

    Conclusion

    In this post you have learned about the multiple ways of Lombok Project configuration in you eclipse IDE.

    Related Posts

    Your Feedback Motivate Us

    If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

    Happy Learning !!!

    Ant Lombok Configuration

    In your Ant based application, you can follow the below Ant Lombok Configuration by adding the following Ant dependencies in your build.xml.

    Prerequisites

    These are the pre-requisite of Project Lombok to configure in any applications and IDE.

    Java >= 1.8
    Gradle >= 4.x or Maven 3.6.x
    Project Lombok >= 1.18.20
    
    See Also:

    Lombok Ant Dependency

    You can add below Lombok dependency in you build.xml

    <dependencies>
    	<dependency org="org.projectlombok" name="lombok" rev="1.18.28" conf="build->master" />
    </dependencies>
    

    Lombok Ant Add Jar

    you’ve to add lombok.jar in a lib dir, your javac task would have to look like below

    <javac srcdir="src" destdir="build" source="1.8">
    	<classpath location="lib/lombok.jar" />
    </javac>
    

    Note :you still have to download lombok.jar  and run it as a jar file, if you wish to program in eclipse. The plugin makes that part easier. Follow this link : Eclipse Lombok Configuration

    References

    Related Posts

    Your Feedback Motivate Us

    If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

    Happy Learning !!!

    Gradle Lombok Configuration

    In your Gradle based application, you can follow the below Gradle Lombok Configuration by adding the following maven dependencies in your build.gradle.

    Prerequisites

    These are the pre-requisite of Project Lombok to configure in any applications and IDE.

    Java >= 1.8
    Gradle >= 4.x or Maven 3.6.x
    Project Lombok >= 1.18.20
    
    See Also:

    Lombok Gradle Dependency

    You can add below Lombok dependency in you build.gradle

    dependencies {
    	compileOnly 'org.projectlombok:lombok:1.18.20'
    	annotationProcessor 'org.projectlombok:lombok:1.18.20'
    	
    	testCompileOnly 'org.projectlombok:lombok:1.18.20'
    	testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
    }
    

    Now your application is all set for using the Lombok Annotations by adding above Gradle Lombok Configuration.

    Note :

    you still have to download lombok.jar (or find it in gradle’s caches) and run it as a jar file, if you wish to program in eclipse. The plugin makes that part easier. Follow this link : Eclipse Lombok Configuration

    References

    Related Posts

    Your Feedback Motivate Us

    If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

    Happy Learning !!!

    Maven Lombok Configuration

    In your Maven based application, you can follow the below Maven Lombok Configuration by adding the following maven dependencies in your pom.xml.

    Prerequisites

    These are the pre-requisite of Project Lombok to configure in any applications and IDE.

    Java >= 1.8
    Gradle >= 4.x or Maven 3.6.x
    Project Lombok >= 1.18.20
    
    See Also:

    Lombok Maven Dependency

    You can add below Lombok dependency in you pom.xml

    <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.20</version>
          <scope>provided</scope>
    </dependency>
    

    Lombok Maven Compiler Plugin

    You can add below dependency in maven-compiler-plugin of your pom.xml

    <build>
          <plugins>      
                
                <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.5.1</version>
                      <configuration>
                            <source>11</source> <!-- depending on your project -->
                            <target>11</target> <!-- depending on your project -->
                            <annotationProcessorPaths>
                                  <path>
                                        <groupId>org.projectlombok</groupId>
                                        <artifactId>lombok</artifactId>
                                        <version>1.18.20</version>
                                  </path>                              
                            </annotationProcessorPaths>
                      </configuration>
                </plugin>
    

    Now your application is all set for using the Lombok Annotations by adding above Maven Lombok Configuration.

    Note :

    you still have to download lombok.jar (or find it in Maven’s caches) and run it as a jar file, if you wish to program in eclipse. The plugin makes that part easier. Follow this link : Eclipse Lombok Configuration

    References

    Related Posts

    Your Feedback Motivate Us

    If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

    Happy Learning !!!

    Project Lombok Tutorial

    In this Project Lombok tutorial, you will learn everything about the Lombok configuration, Lombok Annotations & Use Cases with examples. Lombok annotation will help a lot to you application development faster and clean code.

    Why Project Lombok?

    Java is broadly used object-oriented programming language. Java has major drawbacks of writing boilerplate code. To overcome this major drawback, save time and efforts of developers, Project Lombok comes into existence. When using any IDE to generate boilerplate code (getters/getters/constructor/toString()/hashCode() methods) or write is manually, it exist in source code that increase the lines of code, and reduce readability and maintainability of class. While using the Project Lombok annotations will reduce or remove all these boilerplate code.

    What is Project Lombok?

    As per Project Lombok

    “Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.”

    Project Lombok provides annotation-based Java library that allows you to minimize or remove boilerplate code of your application which saves time and effort of developers. Lombok offers various annotations for replacing Java code that is well known for being boilerplate, repetitive, or tedious to write. These annotation makes you Java classes clean for readability and maintainability.

    Example : Lombok annotation @Data on class level can take care of all you class getter/setter, toString() method, constructors (default, parametrize), equals() method and hashCode() method. so specifically no need to write or generate this code. Now from above example you can understand how useful is Project Lombok for save time, effort and maintainability of Java application.

    Features of Project Lombok

    Project Lombok Features

    How Project Lombok works?

    Lombok Project dependency will add to your application which will generate all the required code on compile based on annotations specify in class. So your class source code will be clean however your generated compiled class will have all the required methods as per annotations on class and field levels.

    How Project Lombok works

    How to configure Project Lombok?

    Build Tools Lombok Configuration

    IDE Lombok Configuration

    Project Lombok Packages


    The Project Lombok includes the following packages that are having many Lombok annotations and Java API for different purposes.

    • lombok
    • experimental
    • extern.apachecommons
    • extern.flogger
    • extern.java
    • extern.jbosslog
    • extern.log4j
    • extern.slf4j

    Project Lombok Classes

    ClassDescription
    ConfigurationKeysA container class containing all Lombok configuration keys that do not belong to a specific annotation.
    Project Lombok Classes

    Project Lombok Enums

    EnumDescription
    AccessLevelRepresents an AccessLevel.

    Lombok Annotations & Use Cases

    These are very useful Project Lombok annotations which can be use in any Java based applications. Click on Lombok annotations & Use cases to get example.

    Lombok Annotations & Use cases
    Lom Project Annotations
    AnnotationDescription
    @AllArgsConstructorGenerates an all-args constructor.
    @BuilderThe @Builder is class level annotation. @Builder creates a so-called ‘builder’ aspect to the class or the class that contains a member which is annotated with @Builder.
    @Builder.DefaultThe field annotated with @Default must have an initializing expression; that expression is taken as the default to be used if not explicitly set during building.
    @Builder.ObtainViaPut on a field (in case of @Builder on a type) or a parameter (for @Builder on a constructor or static method) to indicate how Lombok should obtain a value for this field or parameter given an instance; this is only relevant if toBuilder is true.
    @CleanupEnsures the variable declaration that you annotate will be cleaned up by calling its close method, regardless of what happens.
    @CustomLogCauses Lombok to generate a logger field based on a custom logger implementation.
    @DataGenerates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields.
    @EqualsAndHashCodeGenerates implementations for the equals and hashCode methods inherited by all objects, based on relevant fields.
    @EqualsAndHashCode.ExcludeIf present, do not include this field in the generated equals and hashCode methods.
    @EqualsAndHashCode.IncludeConfigure the behavior of how this member is treated in the equals and hashCode implementation; if on a method, include the method’s return value as part of calculating hashCode/equality.
    @GeneratedLombok will eventually automatically add this annotation to all generated constructors, methods, fields, and types.
    @GetterPut on any field to make Lombok build a standard getter.
    @NoArgsConstructorGenerates a no-args constructor.
    @NonNullIf put on a parameter, Lombok will insert a null-check at the start of the method /constructor’s body, throwing a NullPointerException with the parameter’s name as a message.
    @RequiredArgsConstructorGenerates a constructor with required arguments.
    @SetterPut on any field to make Lombok build a standard setter.
    @SingularThe singular annotation is used together with @Builder to create single element ‘add’ methods in the builder for collections.
    @SneakyThrows@SneakyThrow will avoid javac’s insistence that you either catch or throw onward any checked exceptions that statements in your method body declare they generate.
    @SynchronizedAlmost exactly like putting the ‘synchronized’ keyword on a method, except will synchronize on a private internal Object, so that other code not under your control doesn’t meddle with your thread management by locking on your own instance.
    @ToStringGenerates an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields.
    @ToString.ExcludeIf present, do not include this field in the generated toString.
    @ToString.IncludeConfigure the behavior of how this member is rendered in the toString; if on a method, include the method’s return value in the output.
    @valUse val as the type of any local variable declaration (even in a for-each statement), and the type will be inferred from the initializing expression.
    @ValueGenerates a lot of code that fits with a class that is a representation of an immutable entity.
    @varUse var as the type of any local variable declaration (even in a for statement), and the type will be inferred from the initializing expression (any further assignments to the variable are not involved in this type inference).
    @WithPut on any field to make Lombok build a ‘with’ – a withX method that produces a clone of this object (except for 1 field which gets a new value).
    Project Lombok Annotations & Use Cases

    References

    Conclusions

    In this Project Lombok Tutorial you have learned about everything about the Lombok annotations & uses cases with examples. Also learn about the dependencies configurations in applications and plugins configurations in different IDE.

    Related Posts

    Your Feedback Motivate Us

    If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

    Happy Learning !!!

    Java 8: Lambda Expression

    Java 8 introduced lambda expression to move toward functional programming. A lambda expression is an anonymous function that doesn’t have a name and doesn’t belong to any class.

    Where to use the Lambda Expression

    A lambda expression can only be used where the type they are matched against is a single abstract method(SAM) interface or functional interface.

    To use a lambda expression, you can either create your own functional interface or use the predefined functional interface provided by Java.

    Example of a pre-defined interface: Runnable, callable, ActionListener, etc.

    Pre Java 8: Use anonymous inner classes.
    Post-Java 8: Now use lambda expression instead of anonymous inner classes.

    Points to remember

    • Lambda expression is also known as a closure that allows us to treat functionality as a method arguments (passing functions around) or treat code as data.
    • Lambda expression concept was first introduced in the LISP programming language.
    • Lambda expression is used to provide an implementation of a functional interface or Single Method Interface.
    • Lambda expression treated as a function so the compiler does not create .class file.
    • Lambda expression doesn’t need to define a method again for implementation.
    • Lambda expression benefit is less coding.

    Java Lambda expression Syntax

    To create a lambda expression, On the left side of the symbol lambda operator(->) specify input parameters (if there are any), and on the right side place the expression or block of statements.

    
     (parameter_list) -> {function_body}
    

    For example, the lambda expression (x, y) -> x + y specifies that lambda expression takes two arguments x and y and returns the sum of these.

    Note:

    • Optional type declaration: No need to declare the data type of a parameter. The compiler can inference the data type from the value of the parameter.
    • The optional parenthesis around parameter: No needs to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
    • Optional curly braces: For a single line of the statement, No need to use curly braces in the expression body.
    • Optional return keyword: The compiler automatically returns the value if the body has a single expression statement to return the value. Curly braces are required to indicate that expression statement returns a value.

    Here is some Lamda expression example according to a number of arguments.

    No Argument Syntax

    
    ()->{
    //write some statemnet here
    }
    

    One Argument Syntax

    
    (arg1)->{
    //write some statemnet here
    }
    

    Two Argument Syntax

    
    (arg1,arg2)->{
    //write some statemnet here
    }
    

    Method vs Lambda Expression in Java

    A function (or method) in Java has four main parts:
    1. Name
    2. Parameter list
    3. Body
    4. return type.

    A lambda expression has these main parts:
    Lambda expression only has a parameter list and body.
    1. No name – Lambda expression is an anonymous function that doesn’t have a name.
    2. Parameter list
    3. Body – This is the main part of the function where implementation is written.
    4. No return type – Don’t need to write a return statement explicitly. The compiler is able to infer the return type by checking the code.

    Example 1: Lambda Expression with a predefined functional interface

    In this thread, execution example consider both ways legacy and lambda expression to implement the run method and start threads.

    public class PredefinedFunctionalInterfaceExample {
    public static void main(String[] args) {
    
    // Implementing Runnable using anonymous class (legacy way)
    Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
    System.out.println("Thread name : " + Thread.currentThread().getName());
    }
    };
    Thread thread1 = new Thread(runnable1);
    
    // Implementing Runnable using Lambda expression because Runnable having
    // only one abstarct method run()
    Runnable runnable2 = () -&gt; {
    System.out.println("Thread name : " + Thread.currentThread().getName());
    };
    Thread thread2 = new Thread(runnable2);
    
    // Start Threads
    thread1.start();
    thread2.start();
    }
    }
    

    Example 2: lambda Expression with your own functional interface

    In this example, define the functional interface “YourFunctionalInterface” definition by the lambda expression for arguments (a,b). When we call the functional interface method with the argument (120,100) then it will make reference to the given definition of FunctionalInterface and return the result as 220.

    @FunctionalInterface
    interface YourFunctionalInterface
    {
     public int addValues(int a, int b);
    }
    
    public class CalculateClass {
    
       public static void main(String args[]) {
            // lambda expression
        YourFunctionalInterface sum = (a, b) -&gt; a + b;
            System.out.println("Result: "+sum.addValues(120, 100));
        }
    }
    

    Output

    
    Result: 220
    

    Example 3: Lambda Expression with no arguments

    @FunctionalInterface
    interface MyFunctionalInterface {
    //abstarct method with no argument
    public String sayWelcome();
    }
    public class LambdaExpressionExample {
    public static void main(String args[]) {
    //No argument lambda expression
    MyFunctionalInterface msg = () -&gt; {
    return "Welcome to Facing Issues on IT !!";
    };
    System.out.println(msg.sayWelcome());
    }
    }
    

    Output

    
    Welcome to Facing Issues on IT !!
    

    Example 4: Lambda Expression for Collection Iteration

    import java.util.*;
    public class LambdaExpressionLoopExample{
    public static void main(String[] args) {
    List list=new ArrayList();
    list.add("Saurabh");
    list.add("Gaurav");
    list.add("Bharti");
    list.add("Herry");
    list.add("Henry");
    list.forEach(
    // lambda expression for list iteration
    (names)-&gt;System.out.println(names)
    );
    }
    }
    

    Output

    
    Saurabh
    Gaurav
    Bharti
    Herry
    Henry
    

    Note:

    1. As you can see from these examples lambda expression used less code.
    2. Lambda expression is backward compatible so we can enhance our existing API when migrating to Java 8.

    References

    Java 8: Optional for handling NULL

    Java 8 introduces a new class called java.util.Optional to overcome the headache of NullPointerException. Optional class is a type of container of optional single value that either contains a value or doesn’t (it is then said to be “empty”).

    The optional class having lots of methods to deal with different cases:

    Create Optional Object

    Create Optional Object : Empty

    
    Optional optionalObj=Optional.empty();
    

    Create Optional Object: Non-Null
    Optional.of() method throw NullPointerException if passing object reference is null.

    
    YourClass yourObj=new YourClass();
    Optional optionalObj=Optional.of(yourObj);
    

    Create Optional Object: Allowed Null
    Optional.ofNullable() method allowed null reference in Optional object container.

    
    YourClass yourObj=null;
    Optional optionalObj=Optional.ofNullable(yourObj);
    

    Check Optional Object for value present

    Optional.isPresent() method return boolean true/false based on any value present in Optional container.

    
    if(optionalObj.isPresent())
    {
    //do something
    }
    

    Optional Object for value

    if Optional.isPresent() method return true then we get value from Optional object by get() method. If value is not exist in Optional object and trying to call get() method then throw exception as NoSuchElementException

    
    if(optionalObj.isPresent())
    {
    YourClass yourObj=optionalObj.get();
    }
    

    Set Optional Object default value and actions

    We can use Optional.orElse() method which provide default value if Optional object is empty.

    
    YourClass yourObj=optionalObj.orElse(new YourClass("defaut"));
    

    We can use Optional.orElseThrow() method which instread of returning default value if Optional empty , throw an exception:

    
    YourClass yourObj = optionalObj.orElseThrow(IllegalStateException::new);
    

    Java Optional Example 1

    In this example, covered all the above case.

    
    import java.util.Optional;
    public class OptionalExamples {
    
    	public static void main(String args[]) {
    		OptionalExamples java8Tester = new OptionalExamples();
    	      Integer value1 = null;
    	      Integer value2 = new Integer(25);
    
    	      //Optional.ofNullable - allows passed parameter to be null.
    	      Optional firstParam = Optional.ofNullable(value1);
    
    	      //Optional.of - throws NullPointerException if passed parameter is null
    	      Optional secondParam = Optional.of(value2);
    	      System.out.println(java8Tester.sum(firstParam,secondParam));
    	   }
    
    	   public Integer sum(Optional a, Optional b) {
    	      //Optional.isPresent - checks the value is present or not
    
    	      System.out.println("First parameter is present: " + a.isPresent());
    	      System.out.println("Second parameter is present: " + b.isPresent());
    
    	      //Optional.orElse - returns the value if present otherwise returns
    	      //the default value passed.
    	      Integer value1 = a.orElse(new Integer(0));
    
    	      //Optional.get - gets the value, value should be present
    	      Integer value2 = b.get();
    	      return value1 + value2;
    	   }
    
    }
    
    

    Output

    
    First parameter is present: false
    Second parameter is present: true
    25
    

    Java Optional Example 2

    public class OptionalExaple2
    
    	public static void main(String[] args) {
    		Optional completeName = Optional.ofNullable(null);
    		// The isPresent() method returns true if this instance of Optional has
    		// non-null value and false otherwise.
    		System.out.println("Complete Name is set? " + completeName.isPresent());
    		// The orElseGet() method provides the fallback mechanism in case
    		// Optional has null value by accepting the function to generate the
    		// default one.
    		System.out.println("Complete Name: " + completeName.orElseGet(() -&gt; "[Unknown]"));
    		// The map() method transforms the current Optional’s value and returns
    		// the new Optional instance.
    		System.out.println(completeName.map(s -&gt; "Hey " + s + "!").orElse("Hey Unknown!"));
    
    		Optional firstName = Optional.of("Saurabh");
    		System.out.println("First Name is set? " + firstName.isPresent());
    		// The orElse() method is similar to orElseGet() but instead of function
    		// it accepts the default value.
    		System.out.println("First Name: " + firstName.orElseGet(() -&gt; "[Unknown]"));
    		System.out.println(firstName.map(s -&gt; "Hey " + s + "!").orElse("Hey Unnknown!"));
    		System.out.println();
    
    	}
    
    }
    
    

    Output

    
    Complete Name is set? false
    Complete Name: [Unknown]
    Hey Unknown!
    First Name is set? true
    First Name: Saurabh
    Hey Saurabh!
    

    References

     

    Java 8: Base64 Encoding and Decoding

    In Java 8 added new class Base64 for encryption and decryption. It supports three types encoding and decoding:

    • Simple
    • URL
    • MIME

    Note: Passing a null argument to a method of this class will cause a NullPointerException to be thrown.

    Simple

    Uses “The Base64 Alphabets” lying in A-Za-z0-9+/ for encoding and decoding. The encoder does not add any line feed/line separate character in output and decoder rejects all characters out of the Base 64 alphabet.

    Example: Simple Encoding and Decoding

    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    
    public class Base64s {
    
    	public static void main(String[] args) {
        final String text = "Facing Issues On IT in Java 8!";
    
            final String encoded = Base64.getEncoder().encodeToString( text.getBytes( StandardCharsets.UTF_8 ) );
            System.out.println("After Encoding:"+ encoded );
    
            final String decoded = new String(Base64.getDecoder().decode( encoded ),StandardCharsets.UTF_8 );
            System.out.println("After Decoding:"+ decoded );
    	}
    }
    

    Output

    
    After Encoding:RmFjaW5nIElzc3VlcyBPbiBJVCBpbiBKYXZhIDgh
    After Decoding:Facing Issues On IT in Java 8!
    

    URL

    Uses “URL and Filename safe Base64 Alphabet” lying in A-Za-z0-9+_ for encoding and decoding. The encoder does not add any line feed/line separater character in output and decoder rejects all characters out of the Base 64 alphabet.

    Example: URL Encoding and Decoding

    import java.util.Base64;
    
    public class Base64URLExample {
    
    	public static void main(String[] args) {
    		String originalUrl = "https://www.google.co.in/?gfe_rd=cr&amp;ei=dzbFV&amp;gws_rd=ssl#q=java";
    		String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
    		System.out.println("After Encoding:"+ encodedUrl );
    
    		byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl);
    		String decodedUrl = new String(decodedBytes);
    		System.out.println("After Decoding:"+ decodedUrl );
    	}
    
    }
    
    

    Output

    
    After Encoding:aHR0cHM6Ly93d3cuZ29vZ2xlLmNvLmluLz9nZmVfcmQ9Y3ImZWk9ZHpiRlYmZ3dzX3JkPXNzbCNxPWphdmE=
    After Decoding:https://www.google.co.in/?gfe_rd=cr&ei=dzbFV&gws_rd=ssl#q=java
    
    

    MIME

    Uses “The Base64 Alphabet” lying in lying in A-Za-z0-9+/ for encoding and decoding. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return ‘\r’ followed immediately by a linefeed ‘\n’ as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.

    Example: MIME Encoding and Decoding

    import java.io.UnsupportedEncodingException;
    import java.util.Base64;
    import java.util.UUID;
    
    public class Base64Mime {
    
    	public static void main(String[] args) {
    
    		try {
    			StringBuilder stringBuilder = new StringBuilder();
    			// Lets generate some mime input to encode
    			for (int i = 0; i &lt; 10; ++i) {
    				stringBuilder.append(UUID.randomUUID().toString());
    			}
    
    			byte[] mimeBytes = stringBuilder.toString().getBytes(&quot;utf-8&quot;);
    			String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
    			System.out.println(&quot;Base64 Encoded String (MIME) :&quot; + mimeEncodedString);
    
    			byte[] decodedBytes = Base64.getMimeDecoder().decode(mimeEncodedString);
    			String decodedMime = new String(decodedBytes);
    			System.out.println(&quot;Base64 Decoded String (MIME) :&quot; + decodedMime);
    
    		} catch (UnsupportedEncodingException e) {
    			System.out.println(&quot;Error :&quot; + e.getMessage());
    		}
    	}
    
    }
    
    

    Output

    
    Base64 Encoded String (MIME) :YWY1MWRkMDAtNjg0MC00MDE4LTk5YWYtMDE4NTFhYmZkYzA3M2Q0MjU1YjMtNDFiOS00ZmZmLTky
    NjktYzc5YjU2Mzg4OGMyM2IzZTAyN2QtNzhkMC00YzRiLTg3MzgtZWFiMmI3OTdlNmVlMzdmYzQ3
    ZDItYmI2Zi00NmVjLThlYTQtOWUwYWJlODA0M2IwN2I1NzIxNjUtNzJjZC00ODhmLWJkMWUtOWVl
    NGI3YTc5M2NmZjczMjU1MDItMzIyNC00Mjc1LWI2MjQtNTcxZTU3ZmZkZjVhNTdiMmM4NTgtMzFi
    Yi00ZjNlLWI5MWYtZWJkNjc5ODlkOTA2NDQ4MDZiZDQtOWM4Zi00NjJlLWI1ZWUtODZiNWM0MTJm
    MmVjM2JmMjFjMDAtNjUwYi00ZjE0LWI5ZTUtOTY2YjE2NjUzMWQxMGJhYTIzMTAtOTFmMy00OGYz
    LTg1ZTgtZmU3OTEyMjNhODc3
    Base64 Decoded String (MIME) :af51dd00-6840-4018-99af-01851abfdc073d4255b3-41b9-4fff-9269-c79b563888c23b3e027d-78d0-4c4b-8738-eab2b797e6ee37fc47d2-bb6f-46ec-8ea4-9e0abe8043b07b572165-72cd-488f-bd1e-9ee4b7a793cff7325502-3224-4275-b624-571e57ffdf5a57b2c858-31bb-4f3e-b91f-ebd67989d90644806bd4-9c8f-462e-b5ee-86b5c412f2ec3bf21c00-650b-4f14-b9e5-966b166531d10baa2310-91f3-48f3-85e8-fe791223a877
    
    

    References

    Java 8: Arrays Parallel Processing

    Java 8 added lots of new methods to allow parallel processing for arrays. The most frequently used one is parallelSort() which may speed up the arrays sorting on multicore machines.

    Java 8 new methods for Arrays

    • Arrays.parallelSetAll(): Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
    • Arrays.parallelSort(): Sorts the specified array into ascending numerical order.
    • Arrays.parallelPrefix(): Cumulates, in parallel, each element of the given array in place, using the supplied function.

    Example: Arrays Parallel Processing

    In this example uses method parallelSetAll() to fill up arrays with 25000 random values.
    After that, the apply parallelSort() on these arrays values. Here you can see the output of the initial 10 values before and after sorting.

    public class ParallelArrays {
    
    	public static void main(String[] args) {
    		    long[] arrayOfLong = new long [ 25000 ];
                //Fill long array with random numbers parallelly
    	        Arrays.parallelSetAll( arrayOfLong,index -&gt; ThreadLocalRandom.current().nextInt( 1000000 ) );
    			//Print initial 10 values of array
                System.out.println("Before Sorting:Print initial 10 values of array");
    	        Arrays.stream( arrayOfLong ).limit( 10 ).forEach(i -&gt; System.out.print( i + " " ) );
    	        System.out.println();
    			//Parallel Sort Array Values
    	        Arrays.parallelSort( arrayOfLong );
    			//Print initial 10 values of array
    			System.out.println("After Sorting:Print initial 10 values of array");
    	        Arrays.stream( arrayOfLong ).limit( 10 ).forEach(i -&gt; System.out.print( i + " " ) );
    	        System.out.println();
    	}
    
    }
    

    Output

    
    Before Sorting:Print initial 10 values of array
    164965 546280 269106 800751 338598 862392 358814 206345 611726 788465 
    After Sorting: Print initial 10 values of array
    4 13 87 93 94 145 203 281 319 397
    

    References

    Java 8 : Nashorn JavaScript Engine

    Java 8 introduced a new Nashorn Javascript Engine to replace existing Rhino. Nashorn Javascript engine provides 2 to 10 times better performance and it directly compiles the code in memory and passes the bytecode to JVM. Nashorn JavaScript Engine enhanced version of javax.script.ScriptEngine and follows the same set of rules, permitting for Java and JavaScript interoperability.

    Points to remember

    • Use jjs command to run Javascript through Nashhorn Engine.
    • The class name for the Nashorn Javascript engine is jdk.nashorn.api.scripting.NashornScriptEngine.

    Exmaple: Nashorn JavaScript Engine

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    
    public class NashornEngineTest {
    
    	public static void main(String[] args) {
    		try {
    			ScriptEngineManager manager = new ScriptEngineManager();
    			ScriptEngine engine = manager.getEngineByName("JavaScript");
    
    			System.out.println(engine.getClass().getName());
    			System.out.println("Result:"
    					+ engine.eval("function increment() { return 1; }; increment() + 1;"));
    		} catch (ScriptException ex) {
    			ex.printStackTrace();
    		}
    
    	}
    
    }
    

    Output

    
    jdk.nashorn.api.scripting.NashornScriptEngine
    Result:2.0
    

    References

    Java 8:Named Parameters in Reflection

    With Java 8 reflection API added method to get parameters name of the method. For that, you need to make some compile-time configuration as pass the compiler flag: javac -parameters.

    In this example, you will difference in reflection API output with and without named parameter configuration.

    Example: Named Parameter Reflection API

    import java.lang.reflect.Method;
    import java.lang.reflect.Parameter;
    
    public class NamedParameterExample {
    
    	public static void main(String[] args) throws Exception {
    
    		for(Method method :NamedParameterExample.class.getDeclaredMethods()) {
    			System.out.println("\nMethod :"+method.getName()+" Parameters are :");
    			for (final Parameter parameter : method.getParameters()) {
    				System.out.println("Parameter: " + parameter.getName());
    			}
    		}
    	}
    
    	public static void printValues()
    	{
    		//Some statement here
    	}
    
    	public static int addOperation(int A, int b)
    	{
    		return A+b;
    	}
    
    	public static int substractionOperation(int param1 , int param2)
    	{
    		return param1-param2;
    	}
    }
    

    Output: Without Configuration for the named parameter

    
    Method :main Parameters are :
    Parameter: arg0
    
    Method :printValues Parameters are :
    
    Method :addOperation Parameters are :
    Parameter: arg0
    Parameter: arg1
    
    Method :substractionOperation Parameters are :
    Parameter: arg0
    Parameter: arg1
    

    Output: With Configuration for the named parameter

    
    
    Method :main Parameters are :
    Parameter: args
    
    Method :printValues Parameters are :
    
    Method :addOperation Parameters are :
    Parameter: A
    Parameter: b
    
    Method :substractionOperation Parameters are :
    Parameter: param1
    Parameter: param2
    
    

    Eclipse Configuration for Named Parameter

    Go to Window Tab -> Preferences -> Compiler -> Select Checkbox for “Store Information about method parameters (usable via reflection)”.
    As shown in the below screen.

    Java 8 named parameter eclipse setting

    Console Configuration for Named Parameter

    On time on compile with javac pass additional configuration as javac -parameters “class name”.

    Maven Configuration for Named Parameter

    On section of the maven-compiler-plugin:

    
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version>
      <configuration> 
           <compilerArgument>-parameters</compilerArgument>
           <source>1.8</source> 
           <target>1.8</target> 
      </configuration>
    </plugin>
    

    Java 8: Reflection API to get Repeatable Annotation Detail

    Java 8, introduced some new reflection APIs to retrieve annotation related information from different elements types like classes, interfaces, method, type, etc.

    Reflection API provides new method getAnnotionByType() to return array of repeating annotations type.

    Pre-requisite: Java: Annotations

    In previous post Java 8: Repeatable Annotations you have learned about the creation and implementation of repeatable annotation. Let’s take one more example of repeatable annotation and see how to get annotation detail by Java reflection APIs.

    Example: Retrieve Annotation Detail by Reflection API

    As repeating annotations should themselves annotated with @Repeatable(Filetrs.class) annotation. Here Filters is just a holder of Filter’s annotations. The filterable interface defined Filter annotation two times with different values.

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Repeatable;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    public class RepeatingAnnotations {
    	@Target(ElementType.TYPE)
    	@Retention(RetentionPolicy.RUNTIME)
    	public @interface Filters {
    		Filter[] value();
    	}
    
    	@Target(ElementType.TYPE)
    	@Retention(RetentionPolicy.RUNTIME)
    	@Repeatable(Filters.class)
    	public @interface Filter {
    		String value();
    	};
    
    	@Filter("filter1")
    	@Filter("filter2")
    	public interface Filterable {
    	}
    
    	public static void main(String[] args) {
    
    	    /**
    		*Here reflection API Class.getAnnotationsByType() returns
    		*array of applied annotations
    		*/
    		for (Filter filter : Filterable.class
    				.getAnnotationsByType(Filter.class)) {
    			System.out.println(filter.value());
    		}
    	}
    
    }
    

    Output

    
    filter1
    filter2
    

    Java 8: Repeatable Annotations

    In Java 8 introduced repeatable annotation so that same annotation can be used multiple time on the same declaration and type use. For example, create a custom annotation to implement as repeatable annotation.

    Pre-Requisite : Java: Annotations Tutorial

    See also: Java 8: Reflection API to get Repeatable Annotation Detail

    Create Custom Repeatable Annotation

    import java.lang.annotation.Repeatable;
    @Repeatable(Schedules.class)
    public @interface Schedule {
    String dayOfMonth() default "first";
    String dayOfWeek() default "Mon";
    int hour() default 2;
    }
    

    Create an array of repeatable annotation

    
    package com.common.annotations;
    public @interface Schedules {
    Schedule[] value();
    }
    

    Use of repeatable annotation

    
    package com.common.annotations;
    import java.util.Date;
    public class RepeatableAnnotationExample {
        public static void main(String[] args) {
    
        } 
    
    @Schedule(dayOfMonth="last")
    @Schedule(dayOfWeek="Fri", hour=23)
       public void doPeriodicCleanup() {
         System.out.println("Clean up started at :"+new Date());
       }
    }
    

    Java 8: StringJoiner Class

    In java 8, introduced a new class StringJoiner to join more than one strings with the specified delimiter. This class also provides prefix and suffix to the final delimited String. We can also set default text value if no values added for string joined.

    Constructors

    • StringJoiner(CharSequence delimiter): Constructs a StringJoiner with a delimiter, with no prefix or suffix.
    • StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix): Constructs a StringJoiner with delimiter on values and add prefix and suffix on the final value.

    Example 1: StringJoiner with delimiter (:)

    Here in this example defined StringJoiner with a delimiter (:) to separate values, use StringJoiner.setEmptyValue() method to default value if not data added. Use StringJoiner.add() method to add values on StringJoiner.

    import java.util.StringJoiner;
    
    public class StringJoinerExample {
    
    	public static void main(String[] args) {
    		// Passing Hyphen(:) as delimiter
            StringJoiner myFriends = new StringJoiner(":");
            myFriends.setEmptyValue("No Friends Found");
            System.out.println("Friends Detail : When no data added");
            System.out.println(myFriends);
            // Joining multiple strings by using add() method
            myFriends.add("Saurabh");
            myFriends.add("Gaurav");
            myFriends.add("Raghav");
            myFriends.add("Shailesh");
            myFriends.add("Ankur");                 
    
            System.out.println("Friends Detail : After data added");
            // Displaying the output String with delimiter :
            System.out.println(myFriends);
    	}
    
    }
    
    

    Output

    
    Friends Detail : When no data added
    No Friends Found
    Friends Detail : After data added
    Saurabh:Gaurav:Raghav:Shailesh:Ankur
    

    Example 2: StringJoiner with a delimiter (:), prefix, and suffix

    In this example, defined StringJoiner with a delimiter for values, prefix, and suffix for final delimited String.

    import java.util.StringJoiner;
    
    public class StringJoinerPrefixSuffixExample {
    
    	public static void main(String[] args) {
    		// Passing colon (:) as delimiter , prefix ({) and suffix (})
            StringJoiner myFriends = new StringJoiner(":","{","}"); 
    
            myFriends.setEmptyValue("No Friends Found");
            System.out.println("Friends Detail : When no data added");
            System.out.println("Friends Detail :"+myFriends);
            // Joining multiple strings by using add() method
            myFriends.add("Saurabh");
            myFriends.add("Gaurav");
            myFriends.add("Raghav");
            myFriends.add("Shailesh");
            myFriends.add("Ankur");
            System.out.println("Friends Detail : After data added");
            // Displaying the output String with delimiter :
            System.out.println(myFriends);
    	}
    
    }
    

    Output

    
    Friends Detail : When no data added
    Friends Detail :No Friends Found
    Friends Detail : After data added
    {Saurabh:Gaurav:Raghav:Shailesh:Ankur}
    

    Example 3: Merge StringJoiner 1 with a delimiter (:), StringJoiner 2 with a delimiter (,)

    We can use StringJoiner.merge() method to merge the values of two StringJoiner. Here also use StringJoiner.length() method which returns the length of the final delimited String.

    import java.util.StringJoiner;
    
    public class StringJoinerMergeExample {
    
    	public static void main(String[] args) {
    	   // Passing Hyphen(:) as delimiter
            StringJoiner myFriends = new StringJoiner(":");
            // Joining multiple strings by using add() method
            myFriends.add("Saurabh");
            myFriends.add("Gaurav");
            myFriends.add("Raghav");
            myFriends.add("Shailesh");
            myFriends.add("Ankur");                 
    
            System.out.println("Friends Detail 1: After data added");
            // Displaying the output String with delimiter :
            System.out.println(myFriends); 
    
         // Passing Hyphen(:) as delimiter
            StringJoiner myFriends1 = new StringJoiner(",");
            // Joining multiple strings by using add() method
            myFriends1.add("Saurabh1");
            myFriends1.add("Gaurav1");
            myFriends1.add("Raghav1");
            myFriends1.add("Shailesh1");
            myFriends1.add("Ankur1");                 
    
            System.out.println("Friends Detail 2: After data added");
            // Displaying the output String with delimiter :
            System.out.println(myFriends1); 
    
            //Merge StringJoiner
            myFriends1.merge(myFriends);
            System.out.println("Result after merge Friends Detail 1 &amp; 2:");
            System.out.println(myFriends1);
    
            //Length of String Joiner
            System.out.println(myFriends1.length());
    
    	}
    
    }
    
    

    Output

    
    Friends Detail 1: After data added
    Saurabh:Gaurav:Raghav:Shailesh:Ankur
    Friends Detail 2: After data added
    Saurabh1,Gaurav1,Raghav1,Shailesh1,Ankur1
    Result after merge Friends Detail 1 & 2:
    Saurabh1,Gaurav1,Raghav1,Shailesh1,Ankur1,Saurabh:Gaurav:Raghav:Shailesh:Ankur
    78
    

    References

    Java 8: Functional Interface

    “An interface having exactly one abstract method is called Function Interface. Along with one abstract method can have any number of default and static methods.”

    Points to Remember

    • This is also called as Single Abstract Method Interfaces or SAM Interfaces.
    • The default method having implementation is not considered as an abstract method.
    • An interface declares an abstract method overriding one of the public methods of java.lang.Object is not considered as an abstract method because any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
    • If you declare an interface with @FunctionalInterface then the annotated interface must satisfy the requirements of a functional interface, if not match requirement will get the compile-time issue.

    Note: Compiler will treat an interface as a functional interface if fulfilling conditions of Functional interface regardless of whether or not declared with @FunctionalInterface annotation.

    Where to use Functional Interface

    • Functional Interface uses to represent Lambda Expressions.

    Create your own functional interface

    We can create our own Functional Interface by using @FunctionalInterface or Define interface having only one abstract method.

    @FunctionalInterface
    interface YourFunctionalInterface
    {
     public int addValues(int a, int b);
    }
    

    Use of Functional Interface

    We can use our own functional interface as above or predefined interface also such as Runnable, ActionListener, Comparator, etc. all these having a single abstract method.

    Example: Use your own functional interface

    In this example, define the functional interface “YourFunctionalInterface” definition by the lambda expression for arguments (a,b). When we call the functional interface method with the argument (120,100)  then it will make reference to the given definition of FunctionalInterface and return the result as 220.

    public class CalculateClass {
    
       public static void main(String args[]) {
            // lambda expression
        YourFunctionalInterface sum = (a, b) -&gt; a + b;
            System.out.println("Result: "+sum.addValues(120, 100));
        }
    }
    

    Output

    
    Result: 220
    

    Example: Use a predefined functional interface

    In this thread, execution example consider both ways legacy and lambda expression to implement the run method and start threads.

    public class PredefinedFunctionalInterfaceExample {
    public static void main(String[] args) {
    
    // Implementing Runnable using anonymous class (legacy way)
    Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
    System.out.println("Thread name : " + Thread.currentThread().getName());
    }
    };
    Thread thread1 = new Thread(runnable1);
    
    // Implementing Runnable using Lambda expression because Runnable having
    // only one abstarct method run()
    Runnable runnable2 = () -&gt; {
    System.out.println("Thread name : " + Thread.currentThread().getName());
    };
    Thread thread2 = new Thread(runnable2);
    
    // Start Threads
    thread1.start();
    thread2.start();
    }
    }
    

    References