Java8
Has many improvements like added support for functional programming (anonymous functions, lambdas), added new date time API and the new stream API similar to LINQ queries in C# (map/reduce). Also has new Base64 Encoder/Decoder.
Lambda expressions examples
1 /*
2 javac LambdaExpressionTest.java
3 java -cp . LambdaExpressionTest
4 */
5 public class LambdaExpressionTest{
6 public static void main(String[] args){
7 System.out.println("LambdaExpressionTest");
8 // define implementation
9 // (lambda parameters) -> { // lambda body };
10 TestIt testIt = (stuffx) -> System.out.println("Stuff " + stuffx);
11 testIt.sayStuff("Msg");
12
13 // change lambda function implementation
14 testIt = (stuffxx) -> {
15 stuffxx = stuffxx.toUpperCase();
16 System.out.println("OtherStuff " + stuffxx);
17 };
18 testIt.sayStuff("Msg");
19 }
20
21 // lambda expressions require interfaces to define the expected parameters
22 public interface TestIt{
23 void sayStuff(String stuff);
24 }
25 }
Method reference examples
1 /*
2 Usage of :: to refer to a method. Similar in concept to function pointers/callbacks
3
4 javac MethodReferenceExample.java ASD.java
5 java -cp . MethodReferenceExample
6
7 Interface Consumer<T>
8 Functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
9 */
10
11 import java.util.ArrayList;
12
13 public class MethodReferenceExample{
14 public static void main(String[] args){
15 System.out.println("MethodReferenceExample");
16 ArrayList<String> al = new ArrayList<String>();
17 al.add("Str1");
18 al.add("Str2");
19 al.add("Str3");
20
21 //For each string in the array list call println having as argument the item
22 al.forEach(System.out::println);
23 ASD dummy = new ASD();
24 al.forEach(dummy::showStuff);
25 }
26 }
Stream example
1 /*
2 Allows map/reduce stuff ....
3 javac StreamExample.java
4 java -cp . StreamExample
5 */
6 import java.util.List;
7 import java.util.Arrays;
8 import java.util.stream.Stream;
9 import java.util.stream.Collectors;
10 import java.util.Optional;
11
12 public class StreamExample{
13 public static void main(String[] args){
14 List<Integer> list = Arrays.asList(1,5,6,9,2,5,7,8);
15
16 // get stream from list collection
17 Stream<Integer> stream = list.stream();
18 // filters values from the list where the value is higher or equal than 5
19 Stream<Integer> higherEqualThan5 = stream.filter( val -> val >=5 );
20 // get list of Ints from stream
21 List<Integer> list5 = higherEqualThan5.collect(Collectors.toList());
22 list5.forEach( System.out::println );
23
24 stream = list.stream();
25 // maps values from the list multiplying it's elements by 2
26 Stream<Integer> mulBy2 = stream.map( val ->{Integer res= val*2; return res;} );
27 // get list of Ints from stream
28 List<Integer> listMulBy2 = mulBy2.collect(Collectors.toList());
29 listMulBy2.forEach( System.out::println );
30
31 stream = list.stream();
32 // sum (reduce) all values in the list
33 Optional<Integer> reduce = stream.reduce( (a,b) -> a+b );
34 if(reduce.isPresent() ){
35 System.out.println(reduce.get());
36 }
37 }
38 }
java time
1 import java.time.Instant;
2 import java.time.ZoneId;
3 import java.time.ZonedDateTime;
4 import java.time.format.DateTimeFormatter;
5 // ....
6 Instant instant = Instant.ofEpochMilli(datex.getTime());
7 DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
8 ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
9 formattedDateTime = formatterDateTime.format(dateTime);
Filter example
1 import java.util.List;
2 import java.util.ArrayList;
3 import java.util.stream.Stream;
4 import java.util.stream.Collectors;
5
6 class Main {
7 public static void main(String[] args) {
8 System.out.println("Hello world!");
9
10 List<Integer> list = new ArrayList<Integer>();
11 for(int i=1;i<10;i++){
12 list.add(i);
13 }
14
15 Stream<Integer> s = list.stream();
16 List<Integer> result = s.filter( val -> val%2==0 ).collect(Collectors.toList());
17 result.forEach(item -> System.out.println(item) );
18 }
19 }
GetNumbersImpl.java GetNumbers.java Main.java
GetNumbers.java
GetNumbersImpl.java
1 import java.util.List;
2 import java.util.stream.Stream;
3 import java.util.stream.Collectors;
4
5 public class GetNumbersImpl implements GetNumbers {
6 public GetNumbersImpl(){
7
8 }
9 public List<Integer> getEven(List<Integer> numbers) {
10 Stream<Integer> s = numbers.stream();
11 List<Integer> result = s.filter(val -> val % 2 == 0).collect(Collectors.toList());
12 return result;
13 }
14 }
Main.java
1 import java.util.List;
2 import java.util.ArrayList;
3 import java.util.stream.Stream;
4 import java.util.stream.Collectors;
5
6 class Main {
7
8 public static void main(String[] args) {
9 List<Integer> list = new ArrayList<Integer>();
10 for (int i = 1; i < 10; i++) {
11 list.add(i);
12 }
13
14 GetNumbers gn = new GetNumbersImpl();
15 List<Integer> result = gn.getEven(list);
16 result.forEach(item -> System.out.println(item));
17 }
18 }