java使用guava变形数据结构

在java平常开发中,常常须要使用各类数据结构,在涉及到数据结构之间如何优雅的转换时,咱们能够借助google的guava提供的相关功能来优雅的实现。如下记录一些开发中常常须要使用数据结构的变形,以便使用时方便查阅。 通常咱们的数据结构中存储的为对象,如下举例先构造一个类,用来存放中不一样的数据结构中。java

class Person {
    public String name;
    public int age;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).omitNullValues()
                .add("name", name)
                .add("age", age)
                .toString();
    }
}

提供一个方法来构造一个对象list数据结构

public static Collection<Person> queryPersion(){
        return Lists.newArrayList(
                new Person("kang",30),
                new Person("liu",25),
                new Person("han",22)
        );
    }
//某个属性为null
public static List<Person> queryPersion2(){
        return Lists.newArrayList(
                new Person("kang",30),
                new Person("liu",25),
                new Person("han",22),
                new Person(null,24)
        );
    }
  • 获取一组对象中的某个属性,存入一个list 使用Lists.transform实现
List<Person> persons = queryPersion();
        List<String> peopleNames = Lists.transform(persons, new Function<Person, String>() {
            @Override
            public String apply(Person person) {
                return person.getName();
            }
        });
  • 以优雅的方式过滤有null的值 Iterables.filter第二个参数支持传入一个Predicate接口 Predicates 是 Guava 中与 Predicate 接口配套使用的工具类,提供了一些很是有用的工具类
Collection<Person> matchingPersons = queryPersion2();
        Collection<String> peopleNames =
                Lists.newArrayList(
                        Iterables.filter(
                                Iterables.transform(matchingPersons, new Function<Person, String>() {
                                    @Override
                                    public String apply(Person from) {
                                        return from.getName();
                                    }
                                }), Predicates.notNull()
                        )
                );
  • 使用filter过滤年龄大于25岁的人
Collection<Person> persons = queryPersion();
        List<Person> oldPeople = Lists.newArrayList(Iterables.filter(persons, new Predicate<Person>() {
            public boolean apply(Person person) {
                return person.getAge() >= 25;
            }
        }));
  • 将list数据结构变形为map,将list中对象的某个属性提取出来,变为map中的key (开发中高频使用) 须要注意的是这种使用方式,将list中某个对象的属性变为map的key时,该属性不能重复或者为null
//name重复或者name为null时会报错
Collection<Person> yourList = queryPersion();
        Map<String,Person> mappeds = Maps.uniqueIndex(yourList, new Function<Person,String>() {
            @Nullable
            public String apply(Person from) {
                // do stuff here
                return from.getName();
            }});
  • 将list数据结构变为ImmutableListMultimap 若是list中某个对象的属性会重复时,能够使用以下方法转换,但转换的数据结构是一个ImmutableListMultimap,这是guava中提供的一个数据结构,简单的能够理解为map中的value为一个list
Collection<Person> yourList = queryPersion();
        ImmutableListMultimap<String, Person> mapping = Multimaps.index(yourList, new Function<Person,String>() {
            public String apply(Person input) {
                return input.getName();
            }
        });
  • 高效的建立list 在能够预期一个list元素的个数时,能够使用以下方式建立,避免list在扩容时,形成性能衰减。注意的是这里只是传入一个预期的元素个数,实际状况中list中存放的元素个数并不须要彻底等于预期值
Lists.newArrayListWithExpectedSize(size);
相关文章
相关标签/搜索