Java 10 - New Features

Java 10 - New Features

Local-Variable Type Inference ("var")

we can use the keyword?var?to declare local variables

for example, the following definitions:

var i = 10;
var hello = "Hello world!";
var list = List.of(1, 2, 3, 4, 5);
var httpClient = HttpClient.newBuilder().build();
var status = getStatus();        

For comparison – this is how the definitions look before java 10:

int i = 10;
String hello = "Hello world!";
List<Integer> list = List.of(1, 2, 3, 4, 5);
HttpClient httpClient = HttpClient.newBuilder().build();
Status status = getStatus();        

on the other hand, I will avoid using?var:

  • line 1, you don't save a single character; here, I would stick with?int.
  • line 2,?var?is only minimally shorter than?String?– so I will use String here
  • line 5, I will use the old notation. because, I can't tell offhand what?getStatus()?returns. Is it an?int? A?String? An enum? A value object?

Immutable Collections

java 10 offers the possibility to create unmodifiable wrappers for collection classes.

Here is an example:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
List<Integer> unmodifiable = Collections.unmodifiableList(list);        

If we now try to add an element via the wrapper, we get an?UnsupportedOperationException:

unmodifiable.add(4);

?

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.Collections$UnmodifiableCollection.add(...)
	at ...
        

keep in your mind that, the wrapper does not prevent us from modifying the underlying list. any changes to it will be visible in the wrapper. This is because the wrapper does not contain a copy of the list, but just a view:

list.add(4);
System.out.println("unmodifiable = " + unmodifiable);

?

unmodifiable = [1, 2, 3, 4]
        

List.copyOf(), Set.copyOf(), and Map.copyOf()

With Java 10, we now also have the possibility to create immutable copies of collections. For this purpose, we have the static interface methods?List.copyOf(),?Set.copyOf()?and?Map.copyOf().

If we create such a copy and then modify the original collection, the changes will no longer affect the copy:

List<Integer> immutable = List.copyOf(list);
list.add(4);
System.out.println("immutable = " + immutable);

?

immutable = [1, 2, 3]
        

Note: if you need a modifiable copy of the list, you can use the copy constructor:

List<Integer> copy = new ArrayList<>(list);
        

Collectors.toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap()

The collectors created using?Collectors.toList(),?toSet()?and?toMap()?collect the elements of a Stream into mutable lists, sets and maps. The following example shows the use of these collectors and the subsequent modification of the results:

List<Integer> list = IntStream.rangeClosed(1, 3).boxed().collect(Collectors.toList());
Set<Integer> set = IntStream.rangeClosed(1, 3).boxed().collect(Collectors.toSet());
Map<Integer, String> map = IntStream.rangeClosed(1, 3).boxed()
        .collect(Collectors.toMap(Function.identity(), String::valueOf));

list.add(4);
set.add(4);
map.put(4, "4");

System.out.println("list = " + list);
System.out.println("set  = " + set);
System.out.println("map  = " + map);
        


Optional.orElseThrow()

Optional, introduced in Java 8, provides the?get()?method to retrieve the value wrapped by the?Optional. in Java 10 the method?orElseThrow() is an exact copy of the?get()?method – only the name is different. Since it is clear from the name that this method can throw an exception.

public T get() {
  if (value == null) {
    throw new NoSuchElementException("No value present");
  }
  return value;
}

public T orElseThrow() {
  if (value == null) {
    throw new NoSuchElementException("No value present");
  }
  return value;
}        

thanks Sven Woltmann

Mohamed Mostafa , ITIL? 4,Archeticture

Oracle HRMS Techno Functional-Architect at Wipro

2 年

Thanks for sharing Ahmed El-Sayed

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了