Java 8 Features
Java 8 feature list
1. Lambda Expressions
List<String> names = Arrays.asList("Ravi", "Anil", "Viswa");
names.forEach(n -> System.out.println(n));
👉 Useful for concise functional programming.
2. Method References
names.forEach(System.out::println);
👉 Cleaner syntax when reusing existing methods.
3. Default & Static Methods in Interfaces
interface Vehicle {
default void start() { System.out.println("Starting..."); }
static void info() { System.out.println("Vehicle info"); }
}
👉 Allows evolving APIs without breaking old code.
4. Stream API
List<Integer> nums = Arrays.asList(1,2,3,4,5);
int sum = nums.stream().filter(n -> n%2==0).mapToInt(n->n).sum();
System.out.println(sum); // 6
👉 Declarative data processing.
5. Optional Class
Optional<String> name = Optional.ofNullable(null);
System.out.println(name.orElse("Default")); // Default
👉 Avoids NullPointerException.
6. New Date/Time API
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plusWeeks(1);
System.out.println(nextWeek);
👉 Immutable, thread-safe time handling.
7. CompletableFuture
CompletableFuture.supplyAsync(() -> "Hello")
.thenAccept(System.out::println);
👉 Asynchronous programming made easy.
8. Nashorn JavaScript Engine
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello from JS')");
👉 Embeds JavaScript in JVM.
9. Base64 Encoding/Decoding
String encoded = Base64.getEncoder().encodeToString("data".getBytes());
System.out.println(encoded);
👉 Secure data transmission.
10. Parallel Array Sorting
int[] arr = {5,3,8,1};
Arrays.parallelSort(arr);
System.out.println(Arrays.toString(arr)); // [1,3,5,8]
👉 Faster sorting on large arrays.
11. Repeating Annotations
@Schedule(day="Monday")
@Schedule(day="Friday")
class Meeting {}
👉 Apply same annotation multiple times.
12. Type Annotations
List<@NonNull String> list = new ArrayList<>();
👉 More precise type checking.
13. Parameter Name Reflection
Method m = MyClass.class.getMethod("myMethod", String.class);
for (Parameter p : m.getParameters()) {
System.out.println(p.getName());
}
👉 Useful for frameworks and debugging.
Java Stream API : Java 8 Feature
📌 Stream API Quick Reference Sheet
| Method | Type | Functional Interface | Purpose | Example |
|---|---|---|---|---|
| forEach() | Terminal | Consumer<T> | Perform an action on each element | list.stream().forEach(System.out::println); |
| filter() | Intermediate | Predicate<T> | Keep only elements that match a condition | list.stream().filter(n -> n > 10); |
| map() | Intermediate | Function<T,R> | Transform each element | list.stream().map(String::length); |
| flatMap() | Intermediate | Function<T,Stream<R>> | Flatten nested structures | list.stream().flatMap(List::stream); |
| sorted() | Intermediate | Comparator<T> | Sort elements | list.stream().sorted(); |
| distinct() | Intermediate | — | Remove duplicates | list.stream().distinct(); |
| limit() | Intermediate | — | Restrict to first n elements | list.stream().limit(5); |
| skip() | Intermediate | — | Skip first n elements | list.stream().skip(3); |
| peek() | Intermediate | Consumer<T> | Debug/log elements | list.stream().peek(System.out::println); |
| reduce() | Terminal | BinaryOperator<T> | Combine into single result | list.stream().reduce(0, Integer::sum); |
| collect() | Terminal | Collector<T,A,R> | Gather results into a collection | list.stream().collect(Collectors.toList()); |
| anyMatch() / allMatch() / noneMatch() | Terminal | Predicate<T> | Check conditions across elements | list.stream().anyMatch(n -> n > 0); |
| findFirst() / findAny() | Terminal | — | Retrieve one element | list.stream().findFirst(); |
forEach in Stream : The forEach() method in Java’s Stream API is used to iterate over each element of the stream and perform an action on it. It’s a terminal operation, meaning once you call forEach(), the stream pipeline is consumed and cannot be reused.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.stream()
.forEach(System.out::println); // Consumer
filter Method : The filter() method in Java’s Stream API is used to select elements that match a given condition. It’s an intermediate operation, meaning it transforms the stream but doesn’t consume it
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> evens = numbers.stream()
.filter(n -> n % 2 == 0) // Predicate
.collect(Collectors.toList());
System.out.println(evens);
Comments
Post a Comment