zoukankan      html  css  js  c++  java
  • java使用guava变形数据结构

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

    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);
    
  • 相关阅读:
    MyEclipse 2015 Stable 2.0破解方法
    GeoGlobe Server运维
    GeoGlobe Server运维
    Silverlight用户无法注册之MySql.Data.dll不一致
    Photoshop影像匀色技术
    GeoGlobe Server使用问题收集
    Windows Server 2008 R2中无法使用360免费Wifi的解决方案
    吉奥平台软件安装经验分享
    U盘中毒后变为快捷方式的解决方法
    主机访问虚拟机中新建的网站
  • 原文地址:https://www.cnblogs.com/kangjianrong/p/8435489.html
Copyright © 2011-2022 走看看