重构到 Java 8Streams 和 Lambdas

Refactoring to Java 8 Streams and Lambdas

Java 在 2020 年庆祝了它的 25 岁生日。1995 年编写的代码今天仍然可以运行,甚至无需重新编译。这是 Java 在企业中取得如此成功的最大原因之一。

多年来,我们已经看到了许多使 Java 代码更易于使用的改进。内部类出现在 Java 1.1 中。Java 1.2 中加入了一个合适的集合框架。Java 5 通过泛型为我们提供了更好的类型安全性。

但是对于 Java 程序员来说,最大的改进来自带有 Streams 和 Lambdas 的 Java 8。我们现在可以以声明式而不是命令式编写 Java 代码。这更好地表达了程序逻辑的“什么”,而不是“如何”。

自 Java 8 以来,我们对 Java 编程语言进行了持续不断的改进。记录、密封类、模式匹配、局部变量类型推断等等。它们都有助于更轻松地编写出色的 Java 代码。

不幸的是,许多 Java 代码库仍停留在 Java 6 的黑暗时代。这需要重构。

在这个为期一天的课程中,我们将学习如何以及何时“重构”,重点关注最大的改进:Java Streams。但是,我们还展示了 Java 编程语言中的其他新内容。

这是一门非常实用的课程。每个部分都有练习,我们可以在其中重构具有 330k LOC 的大型 ERP 系统的现有代码库。

以下是我们将在本次研讨会期间介绍的一些内容

如何在有和没有 IDE 帮助
的情况下重构为流和 lambda 流和 lambda 在 JDK 历史中的位置
什么是函数式接口?
java.util.function 包中的四种核心函数式接口类型
lambda 表示法如何成为基于函数式接口的匿名内部类的简写
lambda 可以根据其复杂性采用长短形式
方法引用作为进一步简化某些形式的 lambda
接口中的默认和静态方法如何使用 lambda 来提高通用性、正确性和可读性
当使用 Map.computeIfAbsent 等方法可能不安全时
流的概念及其与可迭代集合的关系
为什么使用流编码比使用 for/while 循环更自然地遵循算法逻辑
如何使用过滤器、映射、匹配器、收集器、reducer 等创建、转换和终止流
为什么我们应该使用收集器而不是 forEach 从流构建集合
使用 Optional 类来避免空值检查,以及如何将选项与流一起使用
如何使用偷偷摸摸的 throws(没有 Lombok)处理 lambda 中的异常
如何针对基本类型 int、long 和 double 优化功能接口、流和选项

 

1.1 Welcome
1.2 Refactoring
1.3 Setting Up Your IDE
1.4 Building the OFBiz Project
1.5 Analyzing Code for Refactoring in IDEA
2.1 Refactoring to Java 5
2.2 Getting Rid of Unnecessary Boxing:Unboxing
2.3 Changing StringBuffer to StringBuilder
2.4 String.contains() instead of indexOf()
2.5 Replace ‘for’ Loop With Java 5 for-in
2.6 Replace ‘while’ Loop With Java 5 for-in
3.1 More Java 5 Refactorings
3.2 Java 7 Refactorings, Diamond Operator
3.3 Java 7 Collapsing try-catch Blocks
3.4 Java 7 Refactoring to try-with-resource
4.1 Java 8 Static Methods in Interfaces
4.2 Java 8 Default Methods in Interfaces
4.3 How Functional Interfaces Work
4.4 The Four Types of Method References
4.5 Refactoring to Iterable.forEach()
4.6 Refactoring to use Collection.removeIf()
5.1 Introduction to Java 8 Streams
5.2 Stream map(), filter(), collect()
5.3 Refactoring to Lambdas and Method References
5.4 Comparators in Java 8
5.5 Arrays.setAll()
5.6 Single Map Methods getOrDefault(), merge(), putIfAbsent()
6.1 Refactoring ContainerConfig.getContainerPropsFromXML()
6.2 collect() in UtilCache.values()
6.3 map():collect() in UtilDateTime.getTimeZones()
6.4 map():collect() in EntityJoinOperator.freeze()
6.5 Collection.forEach() in EntityJoinOperator.visit()
6.6 map():filter():collect() in EntitytFinderUtil.createCondition()
6.7 map():filter():collect() in EntityDataLoader.getUrlByComponentList()
6.8 map():collect() in DelegatorContainer.start()
6.9 map():sum() in OagisShipmentServices.oagisReceiveShowShipment()
6.10 Arrays.setAll() in ShoppingListEvents.markCartItemsArray()
7.1 Stream all:any:noneMatch()
7.2 anyMatch() in MapContext.containsKey()
7.3 allMatch() in FileUtil
7.4 allMatch() in EntityJoinOperator.isEmpty()
7.5 anyMatch() in ModelEntity.getHasFieldWithAuditLog()
7.6 allMatch() in ModelEntity.areFields()
7.7 collect() in ModelEntity.getFieldNamesFromFieldVector()
7.8 anyMatch() in ProductPromoWOrker.hasOrderTotalCondition()
7.9 Stream.findFirst:Any
7.10 findFirst() in MapContext.get()
7.11 Creating Stream from Spliterator in findFirst() in UtilObject.getObjectFromFactory()
7.12 findFirst() in ModelRelation
7.13 findFirst() in ShoppingCartItem
7.14 findFirst() in LoadSale.selectedSale()
7.15 Optional in OrderReadHelper.getShippableSizes()
8.1 distinct() in PaymentGatewayServices.capturePaymentsByInvoice()
8.2 filter():collect() in ContainerConfig.getPropertiesWithValue()
8.3 map():sum() in UtilCache.getSizeInBytes()
8.4 Deep Refactoring in UtilMisc.LocaleHolder.getAvailableLocaleList()
8.5 filter():map():collect() in ModelGroupReader.getEntityNamesByGroup()
8.6 map():reduce() in InvoiceServices.updatePaymentApplicationDefBd()
8.7 Collectors.groupingBy() in ModelReader.rebuildResourceHandlerEntities()
8.8 groupingBy():Checked Exception with ModelReader.getEntitiesByPackage()
8.9 Creating Own Spliterator for NodeELResolver.getValue()
8.10 Conclusion

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。