java 使用 Guava 的基础知识
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4256988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The very basics for using Guava
提问by Angela Harms
I totally did google this. But everything I found is about the next step past where I am.
我完全谷歌了这个。但我发现的一切都是关于我现在所在的下一步。
I am returning to programming after a long time away, and learning Java (& OO) for the first time. I asked about reading a file, and someone suggested I not try to learn Java's contortions for this, but instead use Guava. After googling, I found out roughly what Guava is and does (sweet!) and I downloaded it.
离开很长一段时间后,我重新开始编程,并第一次学习 Java (& OO)。我询问了阅读文件的问题,有人建议我不要为此尝试学习 Java 的扭曲,而是使用 Guava。谷歌搜索后,我大致了解了番石榴是什么和做什么(太棒了!)然后我下载了它。
Then I discovered that I don't actually know what "use Guava" means.
然后我发现我实际上并不知道“使用番石榴”是什么意思。
I have used imports (like junit, plus basic classes like, I dunno, Math and stuff), and I expect it will be similar. But I don't know, for example, what folder to put Guava in, nor whether Eclipse will recognize it. (Maybe that's the only thing I need to know? I don't know that either.)
我使用过导入(比如 junit,加上基本类,比如,我不知道,Math 和其他东西),我希望它会是相似的。但是我不知道,比如把Guava放在什么文件夹里,也不知道Eclipse会不会识别。(也许这是我唯一需要知道的?我也不知道。)
What I am asking for here is this: How do I go from awareness of a new tech (namely Guava libraries) to having code written using that tech, in Eclipse, and executing?
我在这里要求的是:我如何从了解新技术(即 Guava 库)到使用该技术在 Eclipse 中编写代码并执行?
回答by Sean Patrick Floyd
Guava Sample Usage
番石榴示例用法
Yes, using Guava is a great idea, especially for simple tasks as reading a file:
是的,使用 Guava 是一个好主意,尤其是对于读取文件这样的简单任务:
String contents = Files.toString(file, Charsets.UTF_8)
when compared to plain java versions like this:
与这样的普通 Java 版本相比:
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
try {
in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
sb.append(str).append('\n');
}
} catch (IOException e) {
// do something here
} finally{
// and this should also be in a try / catch block
if(in!=null)in.close();
}
return sb.toString();
(But actually you need to have developed Java for years to really appreciate the easiness IMHO.)
(但实际上,您需要开发 Java 多年才能真正体会到恕我直言的易用性。)
Configuring Eclipse to use Guava
配置 Eclipse 以使用 Guava
The easiest way to deal with such a library in Eclipse is to create a User library and add the user library to your project (right click project: Build Path -> Add Library -> User Library
).
在 Eclipse 中处理此类库的最简单方法是创建一个用户库并将用户库添加到您的项目中(右键单击项目:)Build Path -> Add Library -> User Library
。
Here is a short guide to generating a User Library:
以下是生成用户库的简短指南:
- Open the menu entry
Preferences -> Java -> Build Path -> User Libraries
- Press
New...
, enter a name (e.g. 'Guava') - Press
Add JARs...
and search for the JAR files you downloaded
(Unzip the Guava archivebeforehand, inside you'll find the fileguava-r07.jar
. Select that in this dialog, preferably after moving it to a more desirable location) - Now select
Source Attachment -> Edit...
, and find the path to the fileguava-src-r07
. - If you want to, you can repeat this process for the JavaDoc location, pointing it to the
javadoc
folder from the guava distribution - Press
OK
.
- 打开菜单项
Preferences -> Java -> Build Path -> User Libraries
- 按
New...
,输入名称(例如“番石榴”) - 按下
Add JARs...
并搜索您下载的 JAR 文件
(事先解压缩Guava 存档,在里面您会找到该文件guava-r07.jar
。在此对话框中选择该文件,最好将其移动到更理想的位置后) - 现在选择
Source Attachment -> Edit...
并找到文件的路径guava-src-r07
。 - 如果需要,您可以对 JavaDoc 位置重复此过程,将其指向
javadoc
guava 发行版中的文件夹 - 按
OK
。
Now you should be all set, you can select the user library and add it to a project of your choice.
现在您应该已准备就绪,您可以选择用户库并将其添加到您选择的项目中。
Using Maven
使用 Maven
For completeness: being an advocate of dependency management with maven I would of course suggest to manage project dependencies with mavenand m2eclipseinstead of with user libraries.
为了完整性:作为使用 maven 进行依赖管理的倡导者,我当然会建议使用maven和m2eclipse而不是用户库来管理项目依赖关系。
回答by Niraj Tolia
As Sean mentioned, I would highly recommend that you use Maven to import the libraries. It makes it simple to upgrade dependencies later and, once you install m2eclipse, Eclipse will pick the Guava libraries up for you. All you need in your Maven pom.xml to "use Guava" is something along the lines of the following in your dependencies section.
正如肖恩所提到的,我强烈建议您使用 Maven 导入库。这使得以后升级依赖项变得很简单,一旦你安装了m2eclipse,Eclipse 就会为你挑选 Guava 库。您在 Maven pom.xml 中“使用 Guava”所需的全部内容是您的依赖项部分中的以下内容。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>