guava的常用功能

package com.summary;

import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.learn.bean.Student;

/**
 * 
* @ClassName: CollectionSummary
* @Description: TODO(集合类 Guava学习总结)
* @author hganghui@linewell.com
* @date 2017年12月22日 下午2:26:31
*
 */
public class CollectionSummary {

    List<Student> studentList;

    @Before
    /*
     *List的创建方式
     */
    public void listCreate() {
        Student zs = new Student("张三", "男", 36);
        Student lx = new Student("李四", "男", 21);
        Student ww = new Student("王五", "女", 10);
        Student zl = null;//new Student("赵六", "男", 40);
        studentList = Lists.newArrayList(zs, lx, ww, zl);
    }


    /*
     * 如果为空,用默认值去替代  MoreObjects.firstNonNull
     */
    //@Test
    public void testMoreObjects() {
        Student defaultStudent = new Student("未定义", "男", 15);
        Student student1 = new Student("小明", "男", 15);
        Student student2= null;
        Student s1 = MoreObjects.firstNonNull(student1, defaultStudent);
        Student s2 = MoreObjects.firstNonNull(student2, defaultStudent);    
        System.out.println(s1);
        System.out.println(s2);
    }
    /*
     * 将集合添加到另一个集合 Iterables.addAll(collector, iter);
     */
    //@Test
    public void addIterToCollection() {
        Iterable<String> iter = Lists.newArrayList("a1", "bc", "def");
        Collection<String> collector = Lists.newArrayList();
        collector.add("a");
        Iterables.addAll(collector, iter);
        System.out.println(collector);
    }

    /*
     * Iterables.any( Iterable<T> iterable, Predicate<? super T> predicat) 
     * 用于判断一个集合至少一个元素符合条件  符合元素>=1
     */
    //@Test
    public void testIterablesAny() {
        boolean contains = Iterables.any(studentList, new Predicate<Student>() {
            @Override
            public boolean apply(final Student input) {
                return input.getAge() > 29;
            }
        });
        assertTrue(contains);
    }
    /*
     * 找到一个符合条件的元素.没有抛出异常
     * Iterables.find()
     * 找不到可添加默认的元素 就不抛出异常
     * 
     */
    //@Test
    public void testIterablesFinds() {
        Student student = Iterables.find(studentList, new Predicate<Student>() {
            @Override
            public boolean apply(final Student input) {
                return input.getAge() > 13;
            }
        });
        System.out.println(student);
    }

    /*
     *  找出所有符合条件的元素.没有不抛出异常
     * Iterables.filter()
     */
    //@Test
    public void testIterablesFilter() {
        Iterable<Student> students = Iterables.filter(studentList, new Predicate<Student>() {
            @Override
            public boolean apply(final Student input) {
                return input.getAge() > 115;
            }
        });
        System.out.println(students);
    }

    /*
     * 判断集合内的元素是否都符合条件
     * Iterables.all()
     */
    //@Test
    public void testIterablesAll() {
        boolean result = Iterables.all(studentList, new Predicate<Student>() {
            @Override
            public boolean apply(final Student input) {
                return input.getAge() > 5;
            }
        });
        System.out.println(result);
    }

    /*
     * Predicate一些基本使用 还很多
     */
    public void testPredicate() {
        Predicates.alwaysFalse();
        Predicates.alwaysTrue();
        Predicates.isNull();
        Predicates.notNull();
    }
    /*
     * 排序的使用 Ordering  null值的处理
     */
    @Test
    public void testOrdering() {
        Ordering<Student> order = new Ordering<Student>() {     
            @Override
            public int compare(Student left, Student right) {
                if (left.getAge() > right.getAge()) {
                    return 1;
                } else if (left.getAge() == right.getAge()) {
                    return 0;
                } else {
                    return -1;
                }
            }
        };
        //List<Student> list = order.reverse().nullsFirst().sortedCopy(studentList);
        //System.out.println(studentList);
        //System.out.println(list); 
        Collections.sort(studentList, order.nullsFirst());
        System.out.println(studentList);//原集合进行排序   
    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值