栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

Guava2.0—— 重启

Java 更新时间:发布时间: 百科书网 趣学号
 1.背景

公司新员工报道,内部培训资料比较陈旧,需要进行一波更新。java&guava这部分的任务落到了我的头上,所以需要重新备课一下,把这部分教学内容翻新一下,所以这也算是2021年末重制版guava复习。

一方面是和java的对比,jdk7以后到现在的jdk11发生了很多变化,融合了很多guava的东西。

另一方面guava类库内容繁多,想从操作系统层面,将上次程序语言和操作系统操作对接上作为主要方向切入,比如

  • 线程/进程(多线程,并发,锁等)
  • 进而演化到异步(同步/异步/阻塞/非阻塞,listenablefuture/CompletableFuture)
  • 然后到IO操作(网络/磁盘)
  • 到内存(大名鼎鼎的guava cache)
  • 集合运算层面(集合运算,集合增强等,Function/Predicate),字符串操作/数字操作(joiner/splitter等)
  • 时钟上(jodatime)

按照几大类,从API设计,到java中对应的原始操作。从使用示范,到背后设计思想等。

除此之外的,包括形而上的几个技能:

  • 如何读源码
  • 如何看文档
  • 类比,照猫画虎

同时要注意guava的版本,不同版本之间的差异有时候是非常大的。

并发编程网上面也有较好的guava教程,英文版官方教程的中文版本,地址:Google Guava官方教程(中文版) | 并发编程网 – ifeve.com

里面也有分类:

Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] 、字符串处理 [string processing] 、I/O 等等。 所有这些工具每天都在被Google的工程师应用在产品服务中。

查阅Javadoc并不一定是学习这些库最有效的方式。在此,我们希望通过此文档为Guava中最流行和最强大的功能,提供更具可读性和解释性的说明。

看了下这个分类和上面的分类也殊途同归,除了时钟和流等一些内容没有被显示的提及。后面我们也就按照这个思路去逐步剖析guava的设计和实现。 

喜欢且有能力嚼生肉的同学可以移步:Home · google/guava Wiki · GitHub,是英文原版官方文档,对应上面的分类,描述如下:

The Guava project contains several of Google's core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth. Each of these tools really do get used every day by Googlers, in production services.

But trawling through Javadoc isn't always the most effective way to learn how to make best use of a library. Here, we try to provide readable and pleasant explanations of some of the most popular and most powerful features of Guava.

如果可以的话,还是读原版引起的误解最小。另外一个忠告是,因为并发编程网的文章没有随着guava的版本迭代而变化,所以里面一些内容比较过时。比如讲function的时候,function在guava中很早就有了,但是jdk8以后,也引入了function,对于这一点的描述,汉化版里没有体现,但是英文版就有描述:

Java 8 includes the java.util.function and java.util.stream packages, which supercede Guava's functional programming classes for projects at that language level.

While Guava's functional utilities are usable on Java versions prior to Java 8, functional programming without Java 8 requires awkward and verbose use of anonymous classes.

所以还是读原版吧。

2.guava安利文

官方文档少不了对guava的大肆吹捧,当然说的也不无道理,参考:PhilosophyExplained · google/guava Wiki · GitHub

Effective Java item 47, "Know and use the libraries," is our favorite explanation of why using libraries is, by and large, preferable to writing your own utilities. The final paragraph bears repeating:

To summarize, don’t reinvent the wheel. If you need to do something that seems like it should be reasonably common, there may already be a class in the libraries that does what you want. If there is, use it; if you don’t know, check. Generally speaking, library code is likely to be better than code that you’d write yourself and is likely to improve over time. This is no reflection on your abilities as a programmer. Economies of scale dictate that library code receives far more attention than most developers could afford to devote to the same functionality.

We'd also like to mention that:

  • Guava has been battle-tested in production at Google.
  • Guava has staggering numbers of unit tests: as of July 2012, the guava-tests package includes over 286,000 individual test cases. Most of these are automatically generated, not written by hand, but Guava's test coverage is extremely thorough, especially for com.google.common.collect.
  • Guava is under active development and has a strong, vocal, and involved user base.
  • The best libraries seem obvious in retrospect, but achieving this state is notoriously challenging.

主要就是告诉你为什么要用guava,一些老生常谈的内容,“不要重复造轮子啊”,“你能想到的绝大多数功能都有对应的lib”,“lib代码更可靠,经过了更多的测试,也更能与时俱进”,“lib尤其是那些受关注度高的,会享受到开源带来的各种好处,人多力量大”。

A feature is said to have utility when it represents a substantial improvement on the simplest available workaround. In principle, there is always some workaround -- even if it's just writing that utility method, or that data structure, yourself. On the other hand, adding a feature to Guava might

  • save you significant amounts of code
  • avoid forcing you to write code that's difficult to debug, or that's easy to get wrong
  • improve readability
  • improve speed

as compared to the available workarounds.

如果技痒的话,那么就贡献开源社区吧,借助guava已存在的强大架构和代码背景,在此基础上贡献功能会更简单,同时也会有其他人一起参与并维护。

3.guava内容大纲

1.base

Function,Predicate,Optional(Absent/Predicate),Splitter/Joiner

2.cache

整个guava cache设计,缓存设计几要素

3.collect

各种集合视图,和java不同的地方。

4.一些其他的guava文档

Thomas Ferris Nicolaisen - Google Guava

https://www.baeldung.com/whats-new-in-guava-18  guava各个版本新特性,可以替换后面数字切换版本

https://github.com/google/guava/wiki/Release18  guava各个版本新特性,github原版

Optional,Gauva工具及和Java8中实现的区别_诗和远方,任重道远-CSDN博客_guava和java

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/986630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号