Revising current Java features (JAVA-9 to JAVA-14)

Java 9 Features

- Immutable List

List.of(1,2,3);

- Immutable Set

Set.of(1,2,3);

- Immutable Map

Map.of(K1,V1,K2,V2);      // K refers to key and V refers to value
Map.ofEntries(
Map.entry(K1,V1),
Map.entry(K2,V2)
);

- Underscore become a keyword ‘_’

String _ = "alpha";        // compilation error

- Stream modifications :

String key = "";
Stream<String> strng = Stream.ofNullable(key);
Stream<Integer> take = Stream.of(1, 2, 3, 4, 5).map(x -> x * x).takeWhile(x -> x < 3);
Stream<Integer> drop = Stream.of(1, 2, 3, 4, 5)
.map(x -> x * x).dropWhile(x -> x < 2);
Stream.iterate(1, count->count+2 ).forEach(System.out::println); // infinite loop
IntStream.iterate(0, i -> i <10, i-> i++).forEach(System.out::println);

- Optional modifications :

Optional<Integer> optionalValue = Optional
.ofNullable(val)
.or(() -> Optional.of(3));
Optional.ofNullable(val).ifPresentOrElse(
v -> print(v),
Person:: error
);
public static void error() {
System.out.println("Error....");
}
List<String> collect =  Optional
.ofNullable(val)
.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());

- Private method in interface

- Try with resource

try(Person person = new Person()){
...
}
Person person = new Person();
try(person) {
...
}
try(Scanner scanner = new Scanner(new File("txt")); person) {
...
}

Java 10 Features

- Local variable type inference

int a = 10;
var a = 10;

- List.copyOf()

List<Integers> list = Arrays.asList(1,2,3);List<Integer> unmodifiableList = List.copyOf(list);

- Set.copyOf()

List<Integers> list = Arrays.asList(1,2,3);Set<Integer> unmodifiableSet = Set.copyOf(list);

- Map.copyOf()

Map<String,Integer> map = new HashMap<>();
map.put("alpha",1);
map.put("beta",2);
Map<String,Integer> unmodifiableMap = Map.copyOf(map);

- Collectors.toUnmodifiable*()

List<String> strings = Arrays.asList("alpha","beta","gamma");
List<String> collect = strings
.stream()
.collect(Collectors.toUnmodifiableList());

- orElseThrow() method in Optional

List<Integer> integers = Arrays.asList(1, 2, 3, 4);
Integer integer = integers.stream().filter(x -> x > 10).findFirst().orElseThrow();

JAVA 11 Features

- Running java class with a single command

// Till java 10
javac Myclass.java
java Myclass
// From Java 11
java Myclass

- New String methods

String str1 = "";
boolean isStr1Blank = str1.isBlank(); //true
String str2 = null;
boolean isStr2Blank = str1.isBlank(); //java.lang.NullPointerException
String str3 = "alpha";
boolean isStr3Blank = str3.isBlank(); //false
String essay = "line1\nline2\nline3";
System.out.println("Current value:");
System.out.println(essay);
System.out.println("List :");
List<String> collect = essay.lines().collect(Collectors.toList());
System.out.println(collect);
//Current value:
//line1
//line2
//line3
//List:
//[line1, line2, line3]
String className = "  My Class  ";System.out.println(className.strip());              // "MyClass"
System.out.println(className.stripLeading()); // "My Class "
System.out.println(className.stripTrailing()); // " My Class"
String plus = "+";System.out.println(plus.repeat(3));    // +++

- Local variable syntax for lambda expression

(var a, var b) -> a+b;    // valid expressionvar a, var b  -> a +b;    // invalid expression(var a, int b) -> a+b;   // invalid expression

Java 12 Features

- Switch statement

String result = "";
int number = 1;
switch (number) {
case 1:
case 3: {
result = "odd number";
break;
}
case 2:
case 4: {
result = "even number";
break;
}
default: {
throw new NumberFormatException();
}
}
String result = "";
int number = 1;
result = switch (number) {
case 1, 3 -> "odd numbers";
case 2, 4 -> "even numbers";
default -> {
throw new NumberFormatException();
}
};

- File.mismatch method

public static long mismatch(Path path, Path path2) 
throws IOException

- Collectors.teeing()

public static <T, R1, R2, R> Collector<T, ?, R> teeing(
Collector<? super T, ?, R1> downstream1,
Collector<? super T, ?, R2> downstream2,
BiFunction<? super R1, ? super R2, R> merger)
{
return teeing0(downstream1, downstream2, merger);
}
Boolean isEvenDominatedList = Stream.of(1, 2, 3, 4, 5)
.collect(Collectors.teeing(
Collectors
.filtering(x -> x % 2 == 0, Collectors.toList()),
Collectors
.filtering(x -> x % 2 != 0, Collectors.toList()),
(evenList, oddList) -> evenList.size() > oddList.size()
));

- String new Methods

String intro = "My\nname id\nRohan.";

System.out.println(intro);

//My
//name id
//Rohan.
System.out.println(intro.indent(2));// My
// name id
// Rohan.
Integer transform = "x".transform(x -> x.length());
String transform1 = "alpha".transform(x -> x + "beta");
String name = "Rohan"
Optional<String> optionalName = name.describeConstable();
System.out.println(optionalName);
// Optional[Rohan]

Java-13

- Text block

String html = """
<html>
<head>
<link href='/css/style.css' rel='stylesheet' />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>""";

- Modification in switch expression

String result = switch (number) {
case 1,3:
yield "odd";
case 2,4:
yield "even";
default:
throw new NumberFormatException();
};

- New String methods

Java-14 features

- Switch expression

- Pattern matching

if(v instanceof String) {
String value = (String) v;
System.out.println(value);
}
if(v instanceOf String value) {
System.out.println(value);
}

- NullPointerException logs

person.getAddress().getSector().getHouseNumber()
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Address.getSector()" because the return value of "Person.getAddress()" is null.

- Records

record Person() {}
OR
record Person(String firstName, String lastName) {}

Full stack developer