爪哇番石榴图书馆;它最有用和/或隐藏的特性是什么

时间:2020-12-01 20:49:31

I have had a quick scan of the Guava API and the new collection types it provides(Multimap and BiMap for example appear useful) and I am thinking of including the library in the project(s) I work on. However, I also have a reticence to include libraries willy-nilly if they are of no great benefit and learning the features wastes valuable time.

我已经快速浏览了Guava API和它提供的新的集合类型(例如,Multimap和BiMap看起来很有用),我正在考虑在我工作的项目中包括这个库。然而,我也不愿意将库包括在内,如果它们没有什么好处,而且学习这些特性会浪费宝贵的时间。

Have you included the Guava library in your project and has it proved useful in any unexpected way? Would you always use it in the future? What has been its main benefit/time saver? What are its hidden features?

您的项目中是否包含了番石榴图书馆,它是否以任何意想不到的方式被证明是有用的?你以后还会用吗?它的主要优点是什么?它的隐藏特征是什么?

8 个解决方案

#1


153  

Seriously, everything in Guava is useful. I've been using it for quite a while, and am still always discovering something new I can do with it that takes less code than doing it by hand.

说真的,番石榴里的东西都很有用。我已经使用它很长一段时间了,但我仍然发现我可以用它来做一些新的事情,这些事情比手工操作更省事。

Some things others have not really mentioned that I love:

有些事情别人没有提到我爱:

  • Multimaps are just great. Any time you would use something like Map<Foo, Collection<Bar>>, use a multimap instead and save yourself a ton of tedious checking for an existing collection mapped to a key and creating and adding it if it isn't there.
  • 多重映射仅仅是伟大的。任何时候,您都可以使用Map >,而是使用multimap,为映射到键的现有集合保存大量繁琐的检查,并创建并添加它(如果不存在)。 ,>
  • Ordering is great for building Comparators that behave just how you want.
  • 排序对于构建比较器非常有用,它们的行为正好符合您的要求。
  • Maps.uniqueIndex and Multimaps.index: these methods take an Iterable and a Function and build an ImmutableMap or ImmutableListMultimap that indexes the values in the Iterable by the result of applying the function to each. So with a function that retrieves the ID of an item, you can index a list of items by their ID in one line.
  • 地图。uniqueIndex和多重映射。索引:这些方法使用一个Iterable和一个函数,并构建一个ImmutableMap或ImmutableListMultimap,它通过将函数应用到每个函数的结果来索引迭代中的值。因此,使用一个检索项目ID的函数,您可以在一行中根据项目的ID对项目列表进行索引。
  • The functional stuff it provides... filter, transform, etc. Despite the verbosity of using classes for Functions and Predicates, I've found this useful. I give an example of one way to make this read nicely here.
  • 它提供的功能部件……过滤、转换等。尽管对函数和谓词使用类很麻烦,但我发现这很有用。我举了一个例子,说明如何让这个式子在这里读得更好。
  • ComparisonChain is a small, easily overlooked class that's useful when you want to write a comparison method that compares multiple values in succession and should return when the first difference is found. It removes all the tedium of that, making it just a few lines of chained method calls.
  • 比较链是一个小的、容易被忽略的类,当您想要编写一个连续比较多个值的比较方法时非常有用,并且当发现第一个差异时应该返回。它删除了所有的单调,使它仅仅是几行链式的方法调用。
  • Objects.equal(Object,Object) - null safe equals.
  • Object .equal(Object,Object) - null safe equals。
  • Objects.hashCode(Object...) - easy way to get a hash code based on multiple fields of your class.
  • hashcode(对象…)-基于类的多个字段获取散列代码的简单方法。
  • Objects.firstNonNull(Object,Object) - reduces the code for getting a default value if the first value is null, especially if the first value is the result of a method call (you'd have to assign it to a variable before doing this the normal way).
  • firstnonnull(对象、对象)——如果第一个值为null,就减少了获取默认值的代码,特别是如果第一个值是方法调用的结果(在按常规方法进行此操作之前,必须将其分配给一个变量)。
  • CharMatchers were already mentioned, but they're very powerful.
  • 我们已经提到过,但是他们非常强大。
  • Throwables lets you do some nice things with throwables, such as Throwables.propagate which rethrows a throwable if it's a RuntimeException or an Error and wraps it in a RuntimeException and throws that otherwise.
  • “可扔”让你用“可扔”做一些好事,比如“可扔”。如果它是一个RuntimeException或一个错误并将其包装在一个RuntimeException中并将其抛出,则传播将抛出一个可抛出的对象。

I could certainly go on, but I have to get to work. =) Anyway, despite my having listed some things I like here, the fact is that everything in Guava is useful in some situation or another. Much of it is useful very often. As you use it, you'll discover more uses. Not using it will feel a bit like having one hand tied behind your back.

我当然可以继续,但我得开始工作了。无论如何,尽管我在这里列出了一些我喜欢的东西,但事实是番石榴里的所有东西在某些情况下都是有用的。其中很多都非常有用。当你使用它时,你会发现更多的用途。不使用它会感觉有点像把一只手绑在背后。

#2


42  

I've been effectively using Guava for a couple of years, inside Google - and it's wonderful.

在谷歌里面,我已经有效地使用番石榴好几年了,这真是太棒了。

The parts I'm particularly fond of are:

我特别喜欢的部分是:

  • Charsets.* - so simple, so useful
  • 数据集。如此简单,如此有用。
  • Collections
  • 集合
  • IO handling (read a resource completely in a single line, etc)
  • IO处理(在一行中完全读取资源,等等)
  • Splitter/Joiner
  • 分配器/乔伊纳
  • Preconditions
  • 先决条件

#3


28  

I initially used it for collections shorthands. For example, instead of:

我最初用它来收集近处的东西。例如,而不是:

Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long, List<String>>>();

you can do this:

你可以这样做:

Map<String, Map<Long, List<String>>> map = Maps.newHashMap();

It's also easy to populate maps:

填充地图也很容易:

ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");

Now, I have discovered some other useful utilities present in Guava. For example, the CharMatcher class allows you to match sequences of characters. You can do:

现在,我发现了一些其他有用的实用工具在番石榴。例如,CharMatcher类允许您匹配字符序列。你能做什么:

CharMatcher.inRange('a','z').or(inRange('A','Z'));

or

String phoneNumber = CharMatcher.DIGIT.retainFrom("my phone number is 123456789");

#4


21  

CharMatcher's precomputed() method (source) is a nice "hidden feature" I stumbled upon the other day.

matcharcher的预计算()方法(源代码)是我前几天偶然发现的一个很好的“隐藏特性”。

It's really just an optimization, that creates a lookup table (using a bit array), and then simply looks up characters to see if they "match".

它实际上只是一个优化,创建一个查找表(使用位数组),然后简单地查找字符,看看它们是否“匹配”。

It's the kind of hidden optimization you can leverage when you use a library, that you might not have thought of yourself in your own code.

当您使用库时,您可以利用这种隐藏的优化,您可能在自己的代码中没有考虑到这一点。

Of course, if you create a complex CharMatcher, which you plan to use many times, you must remember to call the precomputed() method, like:

当然,如果您创建了一个复杂的CharMatcher(计划多次使用它),您必须记住调用预定义的()方法,比如:

CharMatcher complexMatcher = CharMatcher.anyOf("cat")
                                        .or(CharMatcher.DIGIT)
                                        .or(CharMatcher.WHITESPACE)
                                        .precomputed();

#5


16  

Here's a YouTube video from Google (speaker: Kevin Bourrillion, lead engineer for Google's core Java libraries) which shows the beauty of Google Collections. One thing Google did, which I believe is brilliant, is guarantee Immutability in collections.

下面是谷歌的YouTube视频(演讲者:Kevin Bourrillion,谷歌核心Java库的首席工程师),展示了谷歌收藏的美丽。谷歌所做的一件事,我认为是非常棒的,就是保证收藏的不变性。

#6


12  

Google Guava is a utility library, so I doubt that there is a killer class inside it. The whole things about utility is you almost use that in every projects you have. I can't remember any project I've done that doesn't use Java collection. And truth is, the collection utility of Google Guava is wonderful and should be in the Java SDK itself.

番石榴是一个实用程序库,因此我怀疑里面是否有一个杀手类。关于效用的所有东西,你几乎可以在你的每个项目中使用。我不记得我做过的任何项目不使用Java collection。实际上,谷歌番石榴的收集工具非常棒,应该包含在Java SDK中。

I've written three articles about classes on Google Guava:

我写了三篇关于番石榴类的文章:

And this is not all, there are many other things you can do with Guava.

这还不是全部,番石榴还有很多其他的用途。

#7


8  

Absolutely very super useful. It's almost invariably the first library added to a new project.

绝对非常非常有用。它几乎总是第一个加入新项目的库。

  1. We're very fond of Iterators/Iterables and the Function interface.
  2. 我们非常喜欢迭代器/迭代器和函数接口。
  3. The Service family of interfaces are great abstractions
  4. 接口的服务系列是很好的抽象
  5. We're so committed we've started to use the ImmutableXXX classes our API types to communicate that it can't be changed.
  6. 我们已经开始使用ImmutableXXX类我们的API类型来表示它不能更改。
  7. Computing maps (from MapMaker) are wonderful in certain situations.
  8. 计算地图(来自MapMaker)在某些情况下是很好的。

Overall, the library is very high quality. The API is well thought out, the implementation solid. Highly recommended.

总的来说,图书馆的质量很好。API经过深思熟虑,实现可靠。强烈推荐。

#8


7  

MapMaker now offers bounded LRU caches - that's some substantial machinery hidden behind a tiny API. This has the potential for huge utility, and I'm still all over the code.

MapMaker现在提供有界的LRU缓存——这是隐藏在一个小API后面的一些重要机器。这有巨大的实用价值,我还在研究代码。

#1


153  

Seriously, everything in Guava is useful. I've been using it for quite a while, and am still always discovering something new I can do with it that takes less code than doing it by hand.

说真的,番石榴里的东西都很有用。我已经使用它很长一段时间了,但我仍然发现我可以用它来做一些新的事情,这些事情比手工操作更省事。

Some things others have not really mentioned that I love:

有些事情别人没有提到我爱:

  • Multimaps are just great. Any time you would use something like Map<Foo, Collection<Bar>>, use a multimap instead and save yourself a ton of tedious checking for an existing collection mapped to a key and creating and adding it if it isn't there.
  • 多重映射仅仅是伟大的。任何时候,您都可以使用Map >,而是使用multimap,为映射到键的现有集合保存大量繁琐的检查,并创建并添加它(如果不存在)。 ,>
  • Ordering is great for building Comparators that behave just how you want.
  • 排序对于构建比较器非常有用,它们的行为正好符合您的要求。
  • Maps.uniqueIndex and Multimaps.index: these methods take an Iterable and a Function and build an ImmutableMap or ImmutableListMultimap that indexes the values in the Iterable by the result of applying the function to each. So with a function that retrieves the ID of an item, you can index a list of items by their ID in one line.
  • 地图。uniqueIndex和多重映射。索引:这些方法使用一个Iterable和一个函数,并构建一个ImmutableMap或ImmutableListMultimap,它通过将函数应用到每个函数的结果来索引迭代中的值。因此,使用一个检索项目ID的函数,您可以在一行中根据项目的ID对项目列表进行索引。
  • The functional stuff it provides... filter, transform, etc. Despite the verbosity of using classes for Functions and Predicates, I've found this useful. I give an example of one way to make this read nicely here.
  • 它提供的功能部件……过滤、转换等。尽管对函数和谓词使用类很麻烦,但我发现这很有用。我举了一个例子,说明如何让这个式子在这里读得更好。
  • ComparisonChain is a small, easily overlooked class that's useful when you want to write a comparison method that compares multiple values in succession and should return when the first difference is found. It removes all the tedium of that, making it just a few lines of chained method calls.
  • 比较链是一个小的、容易被忽略的类,当您想要编写一个连续比较多个值的比较方法时非常有用,并且当发现第一个差异时应该返回。它删除了所有的单调,使它仅仅是几行链式的方法调用。
  • Objects.equal(Object,Object) - null safe equals.
  • Object .equal(Object,Object) - null safe equals。
  • Objects.hashCode(Object...) - easy way to get a hash code based on multiple fields of your class.
  • hashcode(对象…)-基于类的多个字段获取散列代码的简单方法。
  • Objects.firstNonNull(Object,Object) - reduces the code for getting a default value if the first value is null, especially if the first value is the result of a method call (you'd have to assign it to a variable before doing this the normal way).
  • firstnonnull(对象、对象)——如果第一个值为null,就减少了获取默认值的代码,特别是如果第一个值是方法调用的结果(在按常规方法进行此操作之前,必须将其分配给一个变量)。
  • CharMatchers were already mentioned, but they're very powerful.
  • 我们已经提到过,但是他们非常强大。
  • Throwables lets you do some nice things with throwables, such as Throwables.propagate which rethrows a throwable if it's a RuntimeException or an Error and wraps it in a RuntimeException and throws that otherwise.
  • “可扔”让你用“可扔”做一些好事,比如“可扔”。如果它是一个RuntimeException或一个错误并将其包装在一个RuntimeException中并将其抛出,则传播将抛出一个可抛出的对象。

I could certainly go on, but I have to get to work. =) Anyway, despite my having listed some things I like here, the fact is that everything in Guava is useful in some situation or another. Much of it is useful very often. As you use it, you'll discover more uses. Not using it will feel a bit like having one hand tied behind your back.

我当然可以继续,但我得开始工作了。无论如何,尽管我在这里列出了一些我喜欢的东西,但事实是番石榴里的所有东西在某些情况下都是有用的。其中很多都非常有用。当你使用它时,你会发现更多的用途。不使用它会感觉有点像把一只手绑在背后。

#2


42  

I've been effectively using Guava for a couple of years, inside Google - and it's wonderful.

在谷歌里面,我已经有效地使用番石榴好几年了,这真是太棒了。

The parts I'm particularly fond of are:

我特别喜欢的部分是:

  • Charsets.* - so simple, so useful
  • 数据集。如此简单,如此有用。
  • Collections
  • 集合
  • IO handling (read a resource completely in a single line, etc)
  • IO处理(在一行中完全读取资源,等等)
  • Splitter/Joiner
  • 分配器/乔伊纳
  • Preconditions
  • 先决条件

#3


28  

I initially used it for collections shorthands. For example, instead of:

我最初用它来收集近处的东西。例如,而不是:

Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long, List<String>>>();

you can do this:

你可以这样做:

Map<String, Map<Long, List<String>>> map = Maps.newHashMap();

It's also easy to populate maps:

填充地图也很容易:

ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");

Now, I have discovered some other useful utilities present in Guava. For example, the CharMatcher class allows you to match sequences of characters. You can do:

现在,我发现了一些其他有用的实用工具在番石榴。例如,CharMatcher类允许您匹配字符序列。你能做什么:

CharMatcher.inRange('a','z').or(inRange('A','Z'));

or

String phoneNumber = CharMatcher.DIGIT.retainFrom("my phone number is 123456789");

#4


21  

CharMatcher's precomputed() method (source) is a nice "hidden feature" I stumbled upon the other day.

matcharcher的预计算()方法(源代码)是我前几天偶然发现的一个很好的“隐藏特性”。

It's really just an optimization, that creates a lookup table (using a bit array), and then simply looks up characters to see if they "match".

它实际上只是一个优化,创建一个查找表(使用位数组),然后简单地查找字符,看看它们是否“匹配”。

It's the kind of hidden optimization you can leverage when you use a library, that you might not have thought of yourself in your own code.

当您使用库时,您可以利用这种隐藏的优化,您可能在自己的代码中没有考虑到这一点。

Of course, if you create a complex CharMatcher, which you plan to use many times, you must remember to call the precomputed() method, like:

当然,如果您创建了一个复杂的CharMatcher(计划多次使用它),您必须记住调用预定义的()方法,比如:

CharMatcher complexMatcher = CharMatcher.anyOf("cat")
                                        .or(CharMatcher.DIGIT)
                                        .or(CharMatcher.WHITESPACE)
                                        .precomputed();

#5


16  

Here's a YouTube video from Google (speaker: Kevin Bourrillion, lead engineer for Google's core Java libraries) which shows the beauty of Google Collections. One thing Google did, which I believe is brilliant, is guarantee Immutability in collections.

下面是谷歌的YouTube视频(演讲者:Kevin Bourrillion,谷歌核心Java库的首席工程师),展示了谷歌收藏的美丽。谷歌所做的一件事,我认为是非常棒的,就是保证收藏的不变性。

#6


12  

Google Guava is a utility library, so I doubt that there is a killer class inside it. The whole things about utility is you almost use that in every projects you have. I can't remember any project I've done that doesn't use Java collection. And truth is, the collection utility of Google Guava is wonderful and should be in the Java SDK itself.

番石榴是一个实用程序库,因此我怀疑里面是否有一个杀手类。关于效用的所有东西,你几乎可以在你的每个项目中使用。我不记得我做过的任何项目不使用Java collection。实际上,谷歌番石榴的收集工具非常棒,应该包含在Java SDK中。

I've written three articles about classes on Google Guava:

我写了三篇关于番石榴类的文章:

And this is not all, there are many other things you can do with Guava.

这还不是全部,番石榴还有很多其他的用途。

#7


8  

Absolutely very super useful. It's almost invariably the first library added to a new project.

绝对非常非常有用。它几乎总是第一个加入新项目的库。

  1. We're very fond of Iterators/Iterables and the Function interface.
  2. 我们非常喜欢迭代器/迭代器和函数接口。
  3. The Service family of interfaces are great abstractions
  4. 接口的服务系列是很好的抽象
  5. We're so committed we've started to use the ImmutableXXX classes our API types to communicate that it can't be changed.
  6. 我们已经开始使用ImmutableXXX类我们的API类型来表示它不能更改。
  7. Computing maps (from MapMaker) are wonderful in certain situations.
  8. 计算地图(来自MapMaker)在某些情况下是很好的。

Overall, the library is very high quality. The API is well thought out, the implementation solid. Highly recommended.

总的来说,图书馆的质量很好。API经过深思熟虑,实现可靠。强烈推荐。

#8


7  

MapMaker now offers bounded LRU caches - that's some substantial machinery hidden behind a tiny API. This has the potential for huge utility, and I'm still all over the code.

MapMaker现在提供有界的LRU缓存——这是隐藏在一个小API后面的一些重要机器。这有巨大的实用价值,我还在研究代码。