0
点赞
收藏
分享

微信扫一扫

Guava系列:FluentIterable使用方法(List条件过滤)

Raow1 2022-01-31 阅读 49
perlidejava


目录

​​场景​​

​​demo​​

场景

list集合过滤数据


demo

package com.nio4444.demo;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.List;

public class FluentIterableDemo {
public static void main(String[] args) {
Person p1 = new Person("12",1) ;
Person p2 = new Person("22",2) ;
Person p3 = new Person("32",3) ;


List perList = Lists.newArrayList(p1,p2,p3);

/**
* filter
*/
Iterable<Person> it = FluentIterable.from(perList).filter(new Predicate<Person>() {
@Override
public boolean apply(Person p) {
return p.age>1;
}
});
System.out.println( Iterables.contains(it,p1));//false
System.out.println( Iterables.contains(it,p2));//true
System.out.println( Iterables.contains(it,p3));//true

/**
* transform: Person-> String
*/
FluentIterable fluentIterablen =FluentIterable.from(perList).transform(new Function<Person,String>() {
@Override
public String apply(Person p) {
return Joiner.on("#").join(p.name,p.age);
}
}) ;

System.out.println(fluentIterablen.toList()); //[12#1, 22#2, 32#3]




}

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

}



举报

相关推荐

0 条评论