Implement java 8 functional interface using lambda example program

  • Functional interfaces introduces in java 8 .
  • Functional interfaces will have single abstract method in it.
  • Check here for full details on Java 8 functional interface with example.
  • Functional interfaces will be implemented by java 8 Lamda expressions.
  • Lets see an example program on how can we use functional interfaces or how can we implement functional interfaces using Lamda expression in java 8.
  • Create one functional interface with one abstract method in it.
  • Create a class and implement this abstract method using Lamda Expressions.



#1: Java example program to create functional interface.

functional interface example in java 8 using lamda expressions


#2: Java example program to implement functional interface using Lamda expressions in java 8.

  1. package com.instanceofjava.java8;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 Lamda expressions
  7.  *
  8.  */
  9. public class LamdaExpressions {

  10. public static void main(String[] args) {
  11. FunctionalInterfaceExample obj = ()->{
  12. System.out.println("hello world");
  13. };

  14. obj.show();
  15. }

  16. }

Output:

  1. hello world

Java 8 functional interface with example

  • Java 8 introduced Lamda expressions and functional interfaces.
  • An interface with one abstract method is known as functional interfaces.
  • Functional interfaces in java 8 marked with @FunctionalInterface.
  • These functional interfaces implementation will be provided by Lamda expressions.
  • Default methods in java 8 interfaces are not abstract methods. But we can define  multiple default methods in functional interface.
  • Java 8 functional interface example program


#1: Java Example program to create java 8 functional interface.


  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {

  11. void show();
  12. }


  • Can we create functional interface with @FunctionalInterface annotation without any single abstract method?
  • No. Functional interfaces must have single abstract method inside it.
  • It will throw compile time error : Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface.
  • Lets see an example program on this so that we can conclude it.
  • How to use these functional interfaces we will discuss in next post on Lamda expressions.
  • Implement java 8 functional interface using lambda example program


#2: What will happend if we define functional interface without a single abstract method in it?

functional interface example in java 8

Java 8 date add n seconds to current date example program

  • Java 8 providing java.time.LocalDateTime class.
  • By using plusSeconds() method of  LocalDateTime we can add seconds to date.
  • By Using minusSeconds() method we can substract  seconds from java date.
  • Lets see an example java program to add n seconds to current date and minus / subtract n seconds from java 8 date. 



#1: Java Example program to add n seconds to current date using java 8.

  1. package com.instanceofjava.java8;
  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: java seconds to java 8 date
  8.  *
  9.  */
  10. public class addSecondsToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13. LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before: "+datetime);
  15. //add seconds by using plusSeconds(seconds) method
  16. datetime=datetime.plusSeconds(12);
  17. System.out.println("After: "+datetime);
  18. }

  19. }

Output:
  1. 2018-02-03T20:19:25.760
  2. 2018-02-03T20:19:37.760

#2: Java Example program to subtract n seconds to current date using java 8.


java 8 date subtract seconds  example program

Java 8 stream filter method example program

  • We can use java 8 stream class filter method to filter the values from a list /map in java
  • By Using filter() and collect() methods of stream class we can achieve this.
  • Lets see an example program to filter value from list without using java 8 streams and with java 8 stream filter.



#1: Java Example program to filter . remove value from list without using java 8 stream

  1. package com.instanceofjava.filtermethodjava8

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;

  5. /**
  6.  * @author www.Instanceofjava.com
  7.  * @category interview programs
  8.  * 
  9.  * Description: Remove value from list without using java 8 stream filter method
  10.  *
  11.  */
  12. public class fillterListJava8{
  13. private static List<String> filterList(List<String> fruits, String filter) {
  14.         List<String> result = new ArrayList<>();
  15.         for (String fruit : fruits) {
  16.             if (!filter.equals(fruit)) { 
  17.                 result.add(fruit);
  18.             }
  19.         }
  20.         return result;
  21.     }
  22. public static void main(String[] args) {
  23. List<String> fruits = Arrays.asList("apple", "banana", "lemon");
  24.  
  25. System.out.println("Before..");
  26.  
  27. for (String str : fruits) {
  28.             System.out.println(str);    
  29.         }
  30.  
  31.         List<String> result = filterList(fruits, "lemon");
  32.         System.out.println("After..");
  33.         for (String str : result) {
  34.             System.out.println(str);    
  35.         }
  36. }
  37. }

Output:
  1. Before..
  2. apple
  3. banana
  4. lemon
  5. After..
  6. apple
  7. banana

#2: Java Example program to filter . remove value from list using java 8 java.util.stream

  1. package com.instanceofjava.java8;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;

  6. /**
  7.  * @author www.Instanceofjava.com
  8.  * @category interview programs
  9.  * 
  10.  * Description: Remove value from list using java 8 stream filter method
  11.  *
  12.  */

  13. public class filterMethodOfStream {

  14. public static void main(String[] args) {
  15. List<String> fruits = Arrays.asList("apple", "banana", "lemon");
  16. String value="lemon";
  17. List<String> result = fruits.stream()                
  18.                 .filter(line -> !value.equals(line))     
  19.                 .collect(Collectors.toList());             
  20.         result.forEach(System.out::println); 
  21.        
  22.         for (String str : result) {
  23.             System.out.println(str);    
  24.         }
  25. }

  26. }






Output:


java 8 stream filter method

How to remove html tags from string java

  • If you want to remove html tags from a string in java you need to use regex.
  • Using java regex we can trim/remove html tags from given string.
  • Lets see a java example program on how can we remove html code or html tags from a string in java.
  • Input String : <B>hello</B>
  • output: hello



#1: Java Example program to remove html tags from a string

  1. package com.instanceofjava.removehtmltags;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: java convert html to plain text/ trim html code from string in java
  7.  *
  8.  */
  9. public class RemoveHtmlTags {

  10. public static void main(String[] args) {
  11. String htmlStr = "<p>Java Program to remove html tags from a String</p>";
  12.         System.out.println(htmlStr);
  13.         htmlStr = htmlStr.replaceAll("\\<.*?\\>", "");
  14.         System.out.println(htmlStr);
  15. }

  16. }

Output:
  1. <p>Java Program to remove html tags from a String</p>
  2. Java Program to remove html tags from a String

remove html code from string in java

Java 8 foreach example program

  • Java 8 introduces for each loop.
  • before that lest see an example of how normal java for each loop.

#1: Java Example program which explain use of for each loop.

  1. package com.instanceofjava.java8;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. public class ForEachLoop {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview questions
  8. * Description: java 8 for each loop
  9. *
  10. */
  11. public static void main(String[] args) {
  12. Map<String, Integer> stumarks = new HashMap<>();
  13. stumarks.put("Sai", 65);
  14. stumarks.put("Vamshi", 65);
  15. stumarks.put("Mahendar", 76);
  16. stumarks.put("Muni", 87);
  17. stumarks.put("Manohar", 90);

  18. for (Map.Entry<String, Integer> entry : stumarks.entrySet()) {
  19. System.out.println(entry.getKey() + " marks : " + entry.getValue());
  20. }

  21. }

  22. }

Output:

  1. Muni marks : 87
  2. Vamshi marks : 65
  3. Mahendar marks : 76
  4. Sai marks : 65
  5. Manohar marks : 90

#2: Java Example program which explains the use of for each loop in java 8

  • In the above program we used for each loop to get key value pairs of map.
  • Same thing will be done in java 8 by using stumarks.forEach((k,v)->System.out.println( k + " marks : " + v));

Select Menu