Data Analytics
27.1K subscribers
1.17K photos
24 videos
28 files
983 links
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
Download Telegram
Data Analytics
# 📚 Java Programming Language – Part 9/10: Collections Framework #Java #Collections #DataStructures #Programming Welcome to Part 9 of our Java series! Today we'll explore the powerful Collections Framework - essential for handling groups of objects efficiently.…
# 📚 Java Programming Language – Part 10/10: Streams & Modern Java Features
#Java #Streams #Lambda #ModernJava #Programming

Welcome to the final part of our Java series! Today we'll explore powerful modern Java features including Streams API and Lambda expressions.

---

## 🔹 Lambda Expressions
Concise way to implement functional interfaces.

### 1. Basic Syntax
// Traditional way
Runnable r1 = new Runnable() {
public void run() {
System.out.println("Running!");
}
};

// Lambda equivalent
Runnable r2 = () -> System.out.println("Running!");


### 2. Lambda Variations
// No parameters
() -> System.out.println("Hello")

// Single parameter
name -> System.out.println("Hello " + name)

// Multiple parameters
(a, b) -> a + b

// With return and body
(x, y) -> {
int sum = x + y;
return sum * 2;
}


---

## 🔹 Functional Interfaces
Single abstract method interfaces work with lambdas.

### 1. Common Functional Interfaces
| Interface | Method | Example Use |
|----------------|----------------|---------------------------|
| Predicate<T> | test(T t) | Filter elements |
| Function<T,R>| apply(T t) | Transform elements |
| Consumer<T> | accept(T t) | Perform actions |
| Supplier<T> | get() | Generate values |

### 2. Practical Example
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// Using Predicate
Predicate<String> startsWithA = name -> name.startsWith("A");
names.stream().filter(startsWithA).forEach(System.out::println);

// Using Function
Function<String, Integer> nameLength = String::length;
names.stream().map(nameLength).forEach(System.out::println);


---

## 🔹 Streams API
Powerful way to process collections functionally.

### 1. Stream Pipeline
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

int sum = numbers.stream() // Source
.filter(n -> n % 2 == 0) // Intermediate operation
.map(n -> n * 2) // Intermediate operation
.reduce(0, Integer::sum); // Terminal operation

System.out.println(sum); // 12 (2*2 + 4*2)


### 2. Common Stream Operations
| Operation | Type | Example |
|-----------|------|---------|
| filter | Intermediate | .filter(n -> n > 5) |
| map | Intermediate | .map(String::toUpperCase) |
| sorted | Intermediate | .sorted(Comparator.reverseOrder()) |
| forEach | Terminal | .forEach(System.out::println) |
| collect | Terminal | .collect(Collectors.toList()) |
| reduce | Terminal | .reduce(0, Integer::sum) |

---

## 🔹 Method References
Shortcut for lambda expressions.

### 1. Types of Method References
// Static method
Function<String, Integer> parser = Integer::parseInt;

// Instance method of object
Consumer<String> printer = System.out::println;

// Instance method of class
Function<String, String> upper = String::toUpperCase;

// Constructor
Supplier<List<String>> listSupplier = ArrayList::new;


---

## 🔹 Optional Class
Handle null values safely.

### 1. Basic Usage
Optional<String> name = Optional.ofNullable(getName());

String result = name
.map(String::toUpperCase)
.orElse("DEFAULT");

System.out.println(result);


### 2. Practical Example
public class UserService {
public Optional<User> findUser(String id) {
// May return null
return Optional.ofNullable(database.findUser(id));
}
}

// Usage:
userService.findUser("123")
.ifPresentOrElse(
user -> System.out.println("Found: " + user),
() -> System.out.println("User not found")
);


---

## 🔹 Modern Java Features (Java 8-17)
### 1. Records (Java 16)
public record Person(String name, int age) {}

// Automatically generates:
// - Constructor
// - Getters
// - equals(), hashCode(), toString()


### 2. Pattern Matching (Java 16)
if (obj instanceof String s) {
System.out.println(s.length());
}


### 3. Text Blocks (Java 15)
String json = """
{
"name": "Alice",
"age": 25
}
""";
1👍1