在google code上,google列出了它的几个核心项目,这些项目涉及到collections, caching, primitives support, concurrency libraries, common annotations, basic string processing, I/O等等方面。

guava就是google在集合框架方面推出的扩展。

目前只是粗略地研究了一下,总得来说,这套api提供的功能不像apache common collections那样全面,但是却轻便灵活,很多api可以带来语法糖一样的便捷性。

看以下几个例子
首先是io方面

File file = new File(getClass().getResource("/test.txt").getFile()); 
    List<String> lines = null
    try
        lines = Files.readLines(file, Charsets.UTF_8); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    
 
&#30452;&#25509;&#30465;&#21435;&#20102;new FileReader(),&#24456;&#26041;&#20415;

再看看这个

FileInputStream in = null;
       try {
        in = new FileInputStream("/data/test.txt");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }finally{
        Closeables.closeQuietly(in);
    }
&#30452;&#25509;&#30465;&#21435;&#20102;try catch&#22359;

还可以把多个io数据源整合成一个

List<InputSupplier<? extends Reader>> list = new ArrayList<InputSupplier<? extends Reader>>();
         
    InputSupplier<? extends Reader> is1 = new InputSupplier<FileReader>(){
        @Override
        public FileReader getInput() throws IOException {
            return new FileReader("/data/f1");
        }
    };
    InputSupplier<? extends Reader> is2 = new InputSupplier<FileReader>(){
        @Override
        public FileReader getInput() throws IOException {
            return new FileReader("/data/f2");
        }
    };
    list.add(is1);
    list.add(is2);
    MultiReader reader = new MultiReader(list.iterator());
    char[] buffer = new char[1024];
    reader.read(buffer,0,100);
&#36825;&#19979;&#23601;&#21487;&#20197;&#20174;&#22810;&#20010;io&#28304;&#20013;&#35835;&#25968;&#25454;&#20102;

对基本类型的操作也提供了。当然这部分功能commons也有,但是guava的api更方便。

int[] array = { 1, 2, 3, 4, 5 }; 
       int a = 4
       boolean contains = Ints.contains(array, a); 
       int indexOf = Ints.indexOf(array, a); 
       int max = Ints.max(array); 
       int min = Ints.min(array); 
       int[] concat = Ints.concat(array, array2);
&#30452;&#25509;&#25226;&#25968;&#32452;&#25340;&#25509;&#12290;

当然还有最重头的,对collection的扩展

List<String> list = new ArrayList<String>(); 
       list.add("a"); 
       list.add("b"); 
       list.add("c"); 
       list.add("d");
&#21487;&#20197;&#36825;&#26679;&#20889;
       ImmutableList<String> of = ImmutableList.of("a", "b", "c", "d");
 
map&#20063;&#19968;&#26679
       Map<String,String> map = new Hasm<String,String>();
       map.put("key1","value1");
       map.put("key2","value2");
&#21487;&#20197;&#20195;&#26367;
       ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");

还有一些特别可爱的语法糖

Map<String,Map<String,? extends Reader>> map = Maps.newHashMap();
Map<String,Map<String,? extends Reader>> map = Maps.newTreeMap();
 
List<? extends Reader> list = Lists.newArrayList();
List<? extends Reader> list = Lists.newLinkedList();
 
&#30452;&#25509;&#30465;&#30053;&#20102;&#35752;&#21388;&#30340;&#27867;&#24418;&#23450;&#20041;&#12290;&#24456;&#26041;&#20415;&#65292;&#20854;&#23454;&#36825;&#20010;&#21407;&#29702;&#24456;&#31616;&#21333;&#65292;
 
       public static <K, V> HashMap<K, V> newHashMap() {
        return new HashMap<K, V>();
       }
&#24456;&#24120;&#35265;&#30340;&#35821;&#27861;&#12290;
springjdbc&#37324;&#30340;rowmapper&#23601;&#26159;&#36825;&#20040;&#20256;&#36882;&#27867;&#24418;&#30340;&#12290;

综合来说,guava是一套非常轻便的api,虽然不像commons collection那样大而全,但是使用guava可以写出很优美的代码,也不失为一种好的选择。对于我们来说,特别有意义的一点在于,这套api代码量很小,便于学习,能够较快地让人掌握java的一些特性。

备注:
使用guava需引入jsr-305的实现。