Java 8 ile Hayatımıza Gelenler

Bayağı bayağı bir gelişme göstermişsin java.. Daha katetmen gereken yol çok ama bu yaptıklarından sonra eski sürümlerine göre gözle görülür bir güzelleşme olmuş. Gözlerim kamaştı doğrusu : ]

Şöyle biraz bakalım mı neler gelmiş;

Önizleme sürümünü; Java™ Platform, Standard Edition 8 Early Access with Lambda Support adresinden indirebilirsiniz. Şimdi şöyle bir göz atalım mı¿

Interface improvements

“forEach” diye bir şey gelmiş ki güzel olmuş.

Lambdas

An extremely valuable property of functional interfaces is that they can be instantiated using lambdas. Here are a few examples of lambdas:

Comma-separated list of inputs with specified types on the left, a block with a return on the right:

(int x, int y) -> { return x + y; }

Comma-separated list of inputs with inferred types on the left, a return value on the right:

(x, y) -> x + y

Single parameter with inferred type on the left, a return value on the right:

x -> x * x

No inputs on left (official name: “burger arrow”), return value on the right:

() -> x

Single parameter with inferred type on the left, a block with no return (void return) on the right:

x -> { System.out.println(x); }

Static method reference:

String::valueOf

Non-static method reference:

Object::toString

Capturing method reference:

x::toString

Constructor reference:

ArrayList::new

The method reference forms are shorthand for the other forms.

Method reference Equivalent lambda expression
String::valueOf x -> String.valueOf(x)
Object::toString x -> x.toString()
x::toString () -> x.toString()
ArrayList::new () -> new ArrayList<>()

A lambda is compatible with a given functional interface when their “shapes” match. By “shapes”, I’m referring to to the types of the inputs, outputs, and declared checked exceptions.

To give a couple of concrete, valid examples:

Comparator<String> c = (a, b) -> Integer.compare(a.length(),
                                                 b.length());

A Comparator<String>’s compare method takes two strings as input, and returns an int. That’s consistent with the lambda on the right, so this assignment is valid.

Runnable r = () -> { System.out.println("Running!"); }

A Runnable’s run method takes no arguments and does not have a return value. That’s consistent with the lambda on the right, so this assignment is valid.

The checked exceptions (if present) in the abstract method’s signature matter too. The lambda can only throw a checked exception if the functional interface declares that exception in its signature.

 

gibi birşeyler demişler. Güzel görünüyor değil mi? Ama şu kötü olmuş sanki;

Non-final variable capture – If a variable is assigned a new value, it can’t be used within a lambda. This code does not compile:

int count = 0;

List<String> strings = Arrays.asList("a", "b", "c");
strings.forEach(s -> {
    count++; // error: can't modify the value of count
});

değil mi? Dediğim kadar varmış olay.

 

Generic type inference improvements

// In Java 7:
foo(Utility<Type>.bar());
Utility.<Type>foo().bar();
// In Java 8:
foo(Utility.bar());
Utility.foo().bar();

diye bir olay var. <Object> ‘in otomatik olarak tamamlanması diye bir olay var sanki değil mi¿

java.time

diye bir paket gelmiş. Oldukça özelliği var, çok ihtiyacımız olacak.

Collections API additions

Here’s a list of the new methods:

  • Iterable.forEach(Consumer)
  • Iterator.forEach(Consumer)
  • Collection.removeAll(Predicate)
  • Collection.spliterator()
  • Collection.stream()
  • Collection.parallelStream()
  • List.sort(Comparator)
  • Map.forEach(BiConsumer)
  • Map.replaceAll(BiFunction)
  • Map.putIfAbsent(K, V)
  • Map.remove(Object, Object)
  • Map.replace(K, V, V)
  • Map.replace(K, V)
  • Map.computeIfAbsent(K, Function)
  • Map.computeIfPresent(K, BiFunction)
  • Map.compute(K, BiFunction)
  • Map.merge(K, V, BiFunction)
  • Map.getOrDefault(Object, V)

Concurrency API additions

  • ForkJoinPool.commonPool()
  • ConcurrentHashMap(v8)
  • The following come with forms for parallel, sequentially, Object, int, long, and double. There are too many of these to link, so see theConcurrentHashMap javadocs for more information.
    • ConcurrentHashMap.reduce…
    • ConcurrentHashMap.search…
    • ConcurrentHashMap.forEach…
  • ConcurrentHashMap.newKeySet()
  • ConcurrentHashMap.newKeySet(int)
  • CompletableFuture
  • StampedLock
  • LongAdder
  • LongAccumulator
  • DoubleAdder
  • DoubleAccumulator
  • CountedCompleter
  • Executors.newWorkStealingPool()
  • Executors.newWorkStealingPool(int)
  • The following come with forms for AtomicReference, AtomicInteger, AtomicLong, and the atomic array versions for each.
    • AtomicReference.getAndUpdate(UnaryOperator)
    • AtomicReference.updateAndGet(UnaryOperator)
    • AtomicReference.getAndAccumulate(V, UnaryOperator)
    • AtomicReference.accumulateAndGet(V, UnaryOperator)

IO/NIO API additions

  • BufferedReader.lines()
  • Files.list(Path)
  • Files.walk(Path, int, FileVisitOption…)
  • Files.walk(Path, FileVisitOption…)
  • Files.find(Path, int, BiPredicate, FileVisitOption…)
  • Files.lines(Path, Charset)
  • DirectoryStream.entries()
  • UncheckedIOException
  • CloseableStream

Reflection and annotation changes

  • type annotations (JSR 308)
  • AnnotatedType
  • Repeatable
  • Method.getAnnotatedReturnType()
  • Field.getAnnotationsByType(Class)
  • Field.getAnnotatedType()
  • Constructor.getAnnotatedReturnType()
  • Parameter – with parameter.getName(), etc.

 

ve daha bir dünya zamazingo var yeni. Bug düzeltmeleri, performans iyileştirmeleri, yeni paketler ve fonksiyonlar (bazıları olmamış olsa da) gelmiş. Güzel görünüyor.

Yoruma Kapalı.