Java Programming, Lambda and more (Java 13, 12, 11, 10, 9,8)
Learn modern Java using Hands-on Step by Step approach. Learn Java 13, Java 12, Java 11, Java10, Java 9, Java 8 features
4.57 (1629 reviews)

42 048
students
12.5 hours
content
Mar 2020
last update
FREE
regular price
Why take this course?
在这个框架中,我们将通过一个银行转账的例子来展示如何使用Java 8及以上版本中的Lambda表达式和Streams。这个例子将涉及到以下几个部分:
- Lambda表达式:我们将看到如何定义和使用Lambda表达式来操作集合中的元素。
- Streams:我们将使用Java 8引入的Stream接口来处理集合数据,包括对元素进行过滤、映射和汇总等操作。
- Parallel Streams:如果数据量很大,我们可以利用并发性能提升来加速流的处理。
- Optional:在处理可能为null的对象时,我们将使用Optional类来避免空引用问题。
- Default和静态方法:我们将探索Java 8中接口的新特性,这些特性可以让我们定义接口中的默认和静态方法,以提供更多的灵活性。
- New DateTime API:在处理金融交易时,准确的日期和时间操作是至关重要的。Java 8引入了新的日期和时间API,我们将使用它来确保正确的交易时间。
- Java Shell (REPL):为了快速测试Lambda表达式、Streams等代码片段,我们可以使用Java Shell。
- Module System:在JDK 9及以上版本中,模块系统被引入。我们将看到如何定义和使用模块。
下面是一个简化的银行转账例子,它展示了如何使用Java 8的特性来处理这个问题:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
public class BankingExample {
// 假设有一个账户类Account,其中包含转账方法transferTo
class Account {
private String accountId;
private double balance;
public Account(String accountId, double balance) {
this.accountId = accountId;
this.balance = balance;
}
public void transferTo(Account toAccount, double amount) {
if (this.balance >= amount) {
this.balance -= amount;
toAccount.deposit(amount);
System.out.println(String.format(
"%s transferred $%,.2f to %s at %s",
this.accountId, amount, toAccount.getAccountId(),
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
} else {
throw new InsufficientFundsException(String.format("%s does not have sufficient funds", this.accountId));
}
}
public void deposit(double amount) {
balance += amount;
}
}
public static List<Account> findAllAccounts() {
// 假设这里返回一个账户列表
return List.of(
new Account("A001", 1000.0),
new Account("A002", 500.0),
// ...其他账户
);
}
public static void main(String[] args) {
List<Account> accounts = findAllAccounts();
// 查找需要转入金额的账户
List<Account> transferOutAccounts = accounts.stream()
.filter(account -> account.getBalance() > 100.0)
.collect(Collectors.toList());
// 查找需要接收金额的账户
List<Account> transferInAccounts = accounts.stream()
.filter(account -> account.getBalance() < 1000.0)
.collect(Collectors.toList());
// 进行转账操作
transferOutAndIn(transferOutAccounts, transferInAccounts);
}
private static void transferOutAndIn(List<Account> transferOutAccounts, List<Account> transferInAccounts) {
transferBetweenAccounts(transferOutAccounts.get(0), transferInAccounts.get(0), 50.0); // 假设取第一个账户进行转账
}
private static void transferBetweenAccounts(Account from, Account to, double amount) {
try {
from.transferTo(to, amount);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}
在这个例子中,我们使用了Lambda表达式来定义账户集合的筛选条件,以及对Streams的操作,如过滤和映射。我们还使用了Java 8的新日期和时间API来记录转账的具体时间。
请注意,这个例子是简化的,实际的银行系统会更加复杂,包括事务管理、异常处理、安全性考虑等。此外,随着Java版本的升级,你可能还会使用JDK 9及以上版本的新特性,如模块化、接口默认方法等。
Loading charts...
Comidoc Review
Our Verdict
Designed for Java developers seeking an up-to-date exploration of the latest language developments, this step-by-step course delivers valuable, practical insights. Address minor shortcomings with heightened attention to supplementary learning materials and sound quality.
What We Liked
- Comprehensive coverage of Java versions 8 through 13, including developer features and real-world applications
- Hands-on, step-by-step approach with live coding and practical use cases for Lambda expressions, Stream Operations, and Optional
- In-depth exploration of functional interfaces, default and static methods, DateTime API, JavaShell, module system, and more
- Engaging delivery by an experienced industry professional offering unique insights into JavaFX, I/O topics, and real-world implementation examples
Potential Drawbacks
- Some concepts could benefit from additional explanation, such as Java 13 preview availability, version transition points, and minor inaccuracies
- Minimal use of quizzes or assignments to supplement learning and assess comprehension
- Low audio volume for certain videos and occasional breaks in sound quality
- Not all real-world implementation examples are provided, potentially impacting full understanding of some concepts
Related Topics
2780630
udemy ID
26/01/2020
course created date
08/02/2020
course indexed date
Bot
course submited by