Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库。

目前主要包含:

  • com.google.common.annotations
  • com.google.common.base
  • com.google.common.collect
  • com.google.common.io
  • com.google.common.net
  • com.google.common.primitives
  • com.google.common.util.concurrent
    在线API doc:http://tool.oschina.net/apidocs/apidoc?api=guava

现有如下Person类:

public class Person {

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

1 将List转化为Map

1.1 转化后具有唯一Key

public class TestMap {
    public static void main(String args[]) {
        List<Person> persons = Arrays.asList(
                new Person("zhang", 15),
                new Person("wang", 16),
                new Person("lee", 18)
        );
        /**
         * 转换后的Map具有唯一键
         */
        Map<String, Person> map = Maps.uniqueIndex(persons, new Function<Person, String>() {
            @Override
            public String apply(Person person) {
                return person.getName();
            }
        });
    }

}

1.2 转化后的Key不唯一

public class TestMap {
    public static void main(String args[]) {
        List<Person> persons = Lists.newArrayList(
                new Person("zhang", 15),
                new Person("zhang", 16),
                new Person("lee", 18)
        );
        /**
         * 转换后的Map有重复键
         */
        Multimap<String, Person> multiMap = Multimaps.index(persons, new Function<Person, String>() {
            public String apply(Person person) {
                return person.getName();
            }
        });
    }
}

2 将Set转化为Map

public class TestMap {
    public static void main(String args[]) {
        Set<Person> persons = Sets.newHashSet(
                new Person("zhang", 15),
                new Person("zhang", 16),
                new Person("lee", 18)
        );
        /**
         * 以Set的值为Key,计算出一个Value
         */
        Map<Person, String> map = Maps.asMap(persons, new Function<Person, String>() {
            public String apply(Person person) {
                return person.getName();
            }
        });
    }
}

3 转换Map的值

public class TestMap {

    public static void main(String args[]) {
        List<Person> persons = Lists.newArrayList(
                new Person("zhang", 15),
                new Person("wang", 15),
                new Person("lee", 18)
        );
        Map<String, Person> map = Maps.uniqueIndex(persons, new Function<Person, String>() {
            public String apply(Person person) {
                return person.getName();
            }
        });

        /**
         * 使用Key和Value作为输入,计算出一个新的Value
         */
        Map<String, Integer> map2 = Maps.transformEntries(map, new Maps.EntryTransformer<String, Person, Integer>() {
            @Override
            public Integer transformEntry(String s, Person person) {
                return person.getAge();
            }
        });

        /**
         * 使用Function计算出一个新的Value
         */
        Map<String, Double> map3 = Maps.transformValues(map, new Function<Person, Double>() {
            @Override
            public Double apply(Person person) {
                return (double)person.getAge();
            }
        });
    }
}

4 筛选Map中符合条件的映射

public class TestMap {

    public static void main(String args[]) {
        List<Person> persons = Lists.newArrayList(
                new Person("zhang", 15),
                new Person("wang", 15),
                new Person("lee", 18)
        );
        Map<String, Person> map = Maps.uniqueIndex(persons, new Function<Person, String>() {
            public String apply(Person person) {
                return person.getName();
            }
        });

        map = Maps.filterEntries(map, new Predicate<Map.Entry<String, Person>>() {
            @Override
            public boolean apply(Map.Entry<String, Person> stringPersonEntry) {
                return stringPersonEntry.getKey() != null && stringPersonEntry.getValue() != null;
            }
        });

        map = Maps.filterKeys(map, new Predicate<String>() {
            @Override
            public boolean apply(String s) {
                return s != null && s.length() > 2;
            }
        });

        map = Maps.filterValues(map, new Predicate<Person>() {
            @Override
            public boolean apply(Person person) {
                return person != null && person.getAge() > 15;
            }
        });


        for (Map.Entry<String, Person> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}