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);//原集合进行排序
}
}
guava的常用功能
最新推荐文章于 2024-09-26 13:59:40 发布