Java 8: Frequently used features

Mohit Talniya
2 min readJul 29, 2018

--

  1. Interfaces can have static and default methods.

http://www.baeldung.com/java-static-default-methods

Static: Available through interface.

Default: Available through implementing class.

Solution : if any class extending these two interfaces which has same default method, class must ovverride default methods to avoid compile error.

Why default methods? — backward compatibility

https://dzone.com/articles/interface-default-methods-java

2. Method References: Object :: methodName

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

3. Functions:

public String add(String string, Function<String, String> fn) {

mreturn fn.apply(string);

}

Function<String, String> fn = parameter -> parameter + " from lambda";

String result = useFoo.add("Message ", fn);

4. What are Lambda expression?

They are anonymous methods used to implement a method defined by a functional interface.

Runnable r= () -> System.out.println("Executing in another thread);
new Thread.start(r);

5. Functional Interfaces:

Functional Interface is just like a regular interface, which permits only one abstract method.

Lambda expression is an instance of a functional interface.

Functional interfaces can also have default methods & static methods along with Single abstract method.

6. Streams:

https://medium.com/@sarathmanchu_9050/java8-what-is-streams-355e922be342

books.stream()
.filter(book -> book.year > 2005) // filter out books published in or before 2005
.map(Book::getAuthor) // get the list of authors for the remaining books
.filter(Objects::nonNull) // remove null authors from the list
.map(Author::getName) // get the list of names for the remaining authors
.forEach(System.out::println); // print the value of each remaining element
--------------------------------------------------------------------List<Users> userList=users;
int total=userList.stream() //create stream instance from collection
.filter(i->i.status()==married) //intermediate operation
.mapToInt(i->i.age()) //intermediate operation
.sum() //Terminal operation kicks off all operations
  1. Sum of array:
int sum =  Arrays.stream(myIntArray)
.sum();

2. Count empty String in Java 8

long count = list.stream().filter(String::isEmpty).count();

3. Optional to return list or null

List<String> listOpt = getList().orElseGet(ArrayList::new);

Advantages of Java 8 Optional:

Null checks are not required.

No more NullPointerException at run-time.

We can develop clean and neat APIs.

No more Boiler plate code

Optional<String> opt = Optional.of("baeldung");

opt.ifPresent(name -> System.out.println(name.length()));

Optional<String> optional = Optional.ofNullable(getString());

Optional<String> opt = Optional.of("baeldung");

opt.ifPresent(name -> System.out.println(name.length()));

orElse : provides value in case of null.

orElseGet: provides functional interfaces

String name = Optional.ofNullable(nullName).orElse("john");

String name = Optional.ofNullable(nullName).orElseGet(() -> "john");

5. forEach in Iterable Interface

l.forEach((a)->System.out.println(a));
---------------------------------------------
myList.forEach(new Consumer<Integer>() {

public void accept(Integer t) {
System.out.println("forEach anonymous class Value::"+t);
}

});

6. Map lets you convert one object type to another object type.

List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());

7. Filter a null value from the List

List<String> result = language.filter(x -> x!=null).collect(Collectors.toList());List<String> result = language.filter(Objects::nonNull).collect(Collectors.toList());

8. Date / Time API : Inspired with Joda time library

http://www.baeldung.com/java-8-date-time-intro

LocalDate tomorrow = LocalDate.now().plusDays(1);

When to use parallelStream()?

https://blog.oio.de/2016/01/22/parallel-stream-processing-in-java-8-performance-of-sequential-vs-parallel-stream-processing/

Internally uses fork-join pool.

myList.parallelStream().sum()

--

--

No responses yet