5 Reasons to update your Java7 to Java8.
Tags: JavaScript, Java, Technoolgy, IoT, It

5 Reasons to update your Java7 to Java8. Keleno Follow Jun 8 · 5 min read

In this article, I have pointed and discussed about five features of Java 8 by which seems more interesting for me.

Stream and other special features

Lambda expressions

Parallel operations

JavaScript engine

New date / time APIs

Stream and other special features

A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements:

List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");

myList

.stream()

.filter(s -> s.startsWith("c"))

.map(String::toUpperCase)

.sorted()

.forEach(System.out::println); // C1 C2

-Stream operations are either intermediate or terminal. Intermediate operations return a stream so we can chain multiple intermediate operations without using semicolons. Terminal operations are either void or return a non-stream result. In the above example filter, map and sorted are intermediate operations whereas forEach is a terminal operation.

Different kind of streams

Streams can be created from various data sources, especially collections. Lists and Sets support new methods stream() and parallelStream() to either create a sequential or a parallel stream.

//Prepare stream from collection

List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");

myList.stream() //Create a stream from a bunch of object references

Stream.of("d2", "a2", "b1", "b3", "c") //Primitive streams (IntStream,LongStream and DoubleStream)

IntStream.range(1, 4)

Other special feature

Extended Annotations Support

It is possible to annotate mostly everything: local variables, generic types, super-classes and implementing interfaces, even the method’s exceptions declaration.

public class Annotations {

@Retention( RetentionPolicy.RUNTIME )

@Target( { ElementType.TYPE_USE, ElementType.TYPE_PARAMETER } )

public @interface NonEmpty {

} public static class Holder< @NonEmpty T > extends @NonEmpty Object {

public void method() throws @NonEmpty Exception {

}

} public static void main(String[] args) {

final Holder< String > holder = new @NonEmpty Holder< String >();

@NonEmpty Collection< @NonEmpty String > strings = new ArrayList<>();

}

}

Internal vs. External Iteration

External iteration (java7)- where a Collection should be implemented with Iterable, So that Iterator can enumerate its elements and clients use this to step sequentially through the elements of a collection.

List<String> alphabets = Arrays.asList(new String[]{"a","b","b","d"}); Iterator<String> iterator = alphabets.listIterator();

while(iterator.hasNext()){

System.out.println(iterator.next().toUpperCase());

} for(String letter: alphabets){

System.out.println(letter.toUpperCase());

}

External iterators may have things to cleanup (like a Thread, Iterator variable)

Internal Iteration (java8):

alphabets.forEach(l -> l.toUpperCase());

advantage of internal iterator is the possibility to implement atomic group operation on the elements of the collection.

Method References

Method references provide the useful syntax to refer directly to exiting methods or constructors of Java classes or objects (instances).