在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

guava 实用操作集合

2017-2-7 13:38| 发布者: zhangjf| 查看: 434| 评论: 0

摘要: guava是 google 几个java核心类库的集合,包括集合、缓存、原生类型、并发、常用注解、基本字符串操作和I/O等等。 大家平时经常遇到某些相同的问题,自己写代码也都能解决。但是久而久之会感觉到很痛苦,因 ...

guava是 google 几个java核心类库的集合,包括集合、缓存、原生类型、并发、常用注解、基本字符串操作和I/O等等。

大家平时经常遇到某些相同的问题,自己写代码也都能解决。但是久而久之会感觉到很痛苦,因为我们一而再,再而三的重复发明轮子。为了不再忍受痛苦,也许我们可以总结自己的类库,但是新的问题来了。自己总结的类库很难与大家分享,不能帮助到更多人。同时自己的类库要不断的进行维护。guava 正是出于这样的目的而来的。

只说不练不行啊,让我们举上一两个例子
判断 String不为null,且不为空

java代码 收藏代码

String str=...;

//use java

if(str !=null && !str.isEmpty()){

//do something

}

//use guava

if(!Strings.isNullOrEmpty(str)){

//do something

}


上而的例子还不是很给力,让我们举一个更给力的例子。复制文件

Java代码 收藏代码

File from=...;

File to=...;

//use java

FileInputStream in=new FileInputStream(from);

FileOutputStream out=new FileOutputStream(to);

byte[] buff=new byte[1024];

int readLength=-1;

while((readLength = in.read(buff)) > 0){

out.write(buff, 0, readLength);

}

in.close();

out.close();

//use guava

Files.copy(from,to); //注意,只用了一行代码噢



通过上面的例子,已经能感觉到guava的强大。接下来,让我们更深入看看guava的其他功能。guava(r09-api)分为这几个包

base 基本的工具类与接口

io io流相关的工具类与方法

net 网络地址相关的工具类与方法

primitives 原始类型的工具类

collect 通用集合接口与实现,与其集合相关工具类

util.concurrent 并发相关工具类



base包

字符串相关工具类
Strings

Java代码 收藏代码

public class StringsTest {

@test

public void test() {

//将空字符串转换为null

Assert.assertEquals(null, Strings.emptyToNull(""));

//将null转换为空字符串

Assert.assertEquals("", Strings.nullToEmpty(null));

//判断字符串为null或者为空

Assert.assertTrue(Strings.isNullOrEmpty("") && Strings.isNullOrEmpty(null));

//将字符串重复

Assert.assertEquals("javajavajava", Strings.repeat("java", 3));

}

}



CharMatcher

Java代码 收藏代码

public class CharMatcherTest {

@Test

public void test() {

String source = "a1b2c3";

CharMatcher matcher = CharMatcher.DIGIT; //预定义的 DIGIT 类型

Assert.assertTrue(mathcer.match('8'));

Assert.assertEquals("123", matcher.retainFrom(source));

Assert.assertEquals(3, matcher.countIn(source));

Assert.assertEquals("abc", matcher.removeFrom(source));

Assert.assertEquals("a2b3c", matcher.trimFrom("1a2b3c4"));

}

}


Jioner

Java代码 收藏代码

public class JoinerTest {

@Test

public void test() {

Assert.assertEquals("2011-08-04", Joiner.on("-").join("2011", "08", "04"));

}

}



Splitter

Java代码 收藏代码

public class SplitterTest {

@Test

public void test() {

Splitter.on(',').split("a,b"); //结果返回Iterable,包含 "a" and "b"

//将结果中的元素trim

//结果依然包含 "a" 和 "b" ,而不是 "a " 和 " b"

Splitter.on(',').trimResults().split("a , b");

//忽略空字符串

//结果必须是"a" 和 "b",而不是 "a" ,"" 和 "b"

Splitter.on(',').omitEmptyStrings().split("a,,b");

}

}



CaseFormat

Java代码 收藏代码

public class CaseFormatTest {

@Test

public void test() throws IOException {

// helloGuava => HELLO_GUAVA

Assert.assertEquals("HELLO_GUAVA", CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "helloGuava"));

// hello-guava => HelloGuava

Assert.assertEquals("HelloGuava", CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, "hello-guava"));

}

}



其他
Preconditions
有些方法在执行前,先要检查传入的参数是否正确,或者类的状态是否正确。通常会这样做

Java代码 收藏代码

if (count <= 0) {

throw new IllegalArgumentException("must be positive: " + count);

}


guava就可以这样,达到相同的效果

Java代码 收藏代码

Preconditions.checkArgument(count > 0, "must be positive: %s", count);



I/O包
ByteStreams提供了针对字节流的工具方法

Java代码 收藏代码

InputStream from=...;

OutputStream to=...;

ByteStreams.copy(from,to); //复制



CharStreams提供了针对字符流的工具方法

Java代码 收藏代码

Reader from =...;

Writer to =...;

CharStreams.copy(from, to); //复制



Files提供了针对文件的工具方法

Java代码 收藏代码

File from=...;

File to=...;

Files.copy(from, to); //复制


Java代码 收藏代码

Files.deleteDirectoryContents(File directory); //删除文件夹下的内容(包括文件与子文件夹)

Files.deleteRecursively(File file); //删除文件或者文件夹

Files.move(File from, File to); //移动文件



Resources提供了针对classpath下资源操作的工具方法

Java代码 收藏代码

URL url = Resources.getResource("config.xml"); //获取classpath根下的config.xml文件url


来自: http://bastengao.iteye.com/blog/1134887

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 00:55

Copyright 2015-2025 djqfx

返回顶部