A series of Java tips and examples, hope you like it.

Classic

Logging

Java 8

FAQs

Some common asked questions.

1. Functional Interface

Java 8 introduced @FunctionalInterface, an interface that has exactly one abstract method. The compiler will treat any interfaces meeting the definition of a functional interface as a functional interface; it means the @FunctionalInterface annotation is optional.

Let us see the six basic function interfaces.

InterfaceSignatureExamples
UnaryOperator<T>T apply(T t)String::toLowerCaseMath::tan
BinaryOperator<T>T apply(T t1, T t2)BigInteger::addMath::pow
Function<T, R>R apply(T t)Arrays::asListInteger::toBinaryString
Predicate<T, U>boolean test(T t, U u)String::isEmptyCharacter::isDigit
Supplier<T>T get()LocalDate::nowInstant::now
Consumer<T>void accept(T t)System.out::printlnError::printStackTrace

2. Lambda Expressions & Method References

Java 8 introduced lambda expressions to provide the implementation of the abstract method of a functional interface.

Further Reading >>> Java 8 Lambda : Comparator example

Review the JDK Iterable class, it has a default method forEach() to accept a function interface ConsumerIterable.java

public interface Iterable<T> {

    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

   //...
}

First, we can provide an anonymous class as the forEach implementation.

List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(new Consumer<String>() {       // anonymous class
    @Override
    public void accept(String str) {
        System.out.println(str);
    }
});

Alternatively, we can use a lambda expression to shorten the code like this:

List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(str -> System.out.println(str)); // lambda expressions

To gain better readability, we can replace lambda expression with method reference.

List<String> list = Arrays.asList("node", "java", "python", "ruby");
list.forEach(System.out::println);           // method references

Further Reading >>> Java 8 method references, double colon (::) operator

Note
Both lambda expression or method reference does nothing but just another way call to an existing method. With method reference, it gains better readability.

3. Streams

4. New Date Time APIs

In the old days, we use Date and Calendar APIs to represent and manipulate date.

  • java.util.Date – date and time, print with default time-zone.
  • java.util.Calendar – date and time, more methods to manipulate date.
  • java.text.SimpleDateFormat – formatting (date -> text), parsing (text -> date) for date and calendar.

Java 8 created a series of new date and time APIs in java.time package. (JSR310 and inspired by Joda-time).

  • java.time.LocalDate – date without time, no time-zone.
  • java.time.LocalTime – time without date, no time-zone.
  • java.time.LocalDateTime – date and time, no time-zone.
  • java.time.ZonedDateTime – date and time, with time-zone.
  • java.time.DateTimeFormatter – formatting (date -> text), parsing (text -> date) for java.time.
  • java.time.Instant – date and time for machine, seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
  • java.time.Duration – Measures time in seconds and nanoseconds.
  • java.time.Period – Measures time in years, months and days.
  • java.time.TemporalAdjuster – Adjust date.
  • java.time.OffsetDateTime – {update me}

Examples…

5. Java 8 Tips

Java 17

JAR Working

Installation

References

Leave a Reply

Your email address will not be published. Required fields are marked *