// Before JDK 8:
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.compareTo(b);
}
});
// With JDK 8 Lambda expression:
Collections.sort(names, (a, b) -> a.compareTo(b));
JDK8-stream API
// Filter a list and sum all even numbers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
long sumOfEvens = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println(sumOfEvens); // Output: 12
JDK8-Optional
// Avoiding null pointer exceptions when dealing with optional valuesOptional<String> optionalName =Optional.ofNullable(getUser().getName());String greeting = optionalName.orElse("Guest");System.out.println("Hello, "+ greeting +"!");// Or chaining methods with Optional
optionalName.ifPresent(name ->System.out.println("Found name: "+ name));
JDK11-本地变量类型推断
/BeforeJDK11:List<String> namesList =newArrayList<>();// With JDK 11 var:var namesList =newArrayList<>();
importjava.util.concurrent.Flow.Subscriber;importjava.util.concurrent.Flow.Subscription;importjava.util.concurrent.ForkJoinPool;importjdk.incubator.foreign.MemorySegment;publicclassVirtualThreadExample{publicstaticvoidmain(String[] args){// Create a virtual threadThread.startVirtualThread(()->{// Your concurrent task here...});// Use Project Loom features like Structured Concurrency or Foreign Function Interface// ...// Alternatively, you can also create a virtual thread poolForkJoinPool virtualThreadPool =ForkJoinPool.commonPool();
virtualThreadPool.submit(()->{// Concurrent task executed on a virtual thread});}}
JDK21-外部函数和内存API
importjdk.incubator.foreign.*;publicclassForeignFunctionExample{publicstaticvoidmain(String[] args)throwsThrowable{// Load a shared libraryLibraryLookup lookup =LibraryLookup.ofLibraryPath("/path/to/library.so");// Define function signatureFunctionDescriptor descriptor =FunctionDescriptor.ofVoid(CLinker.C_CHAR);// Obtain a symbol handle for the functionSymbolHandle symbol = lookup.lookup("my_function").get();// Create a memory segment to hold input dataMemorySegment inputBuffer =MemorySegment.allocateNative(1024,ResourceScope.newConfinedScope());try(ResourceScope scope =ResourceScope.newConfinedScope()){// Call the foreign functionCLinker.systemCLinker().callVoid(symbol, inputBuffer);// Access memory contents after the callbyte resultByte = inputBuffer.getByte(0);System.out.println("Result byte: "+ resultByte);}}}
JDK21-Vector API
import jdk.incubator.vector.*;
public class VectorApiExample {
public static void main(String[] args) {
// Create a vector of integers
VectorSpecies<Integer> intSpec = IntVector.SPECIES_PREFERRED;
VectorMask<Integer> mask = intSpec.indexInRange(0, 4).toMask();
Vector<Integer> vec = Vector.fromArray(intSpec, new int[]{1, 2, 3, 4}, 0);
// Perform vector operations
Vector<Integer> squaredVec = vec.lanewise(VectorOperators.SQUARE);
Vector<Integer> incrementedVec = vec.add(1);
// Extract results back to arrays
int[] squaredArray = new int[4];
int[] incrementedArray = new int[4];
squaredVec.intoArray(squaredArray, 0);
incrementedVec.intoArray(incrementedArray, 0);
System.out.println("Squared array: " + Arrays.toString(squaredArray));
System.out.println("Incremented array: " + Arrays.toString(incrementedArray));
}
}