Guava (FluentIterable迭代器)

package com.youai.learning;

import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println("Guava提供了可以在Iterator中进行处理的功能更丰富的迭代器, 其实就像是加了一个代理, 增加一些功能。");
        List<Emp>emps=new ArrayList<Emp>();
        for(int i=0;i<10;i++){
            emps.add(new Emp(i, "Hello World!",10+i));
        }
        System.out.println("迭代前:");
        for(Emp emp1:emps){
            System.out.println(emp1);
            
        }
        
        
        Iterable<Emp>emps2=FluentIterable.from(emps).filter(new Predicate<Emp>() {

            public boolean apply(Emp emp) {
                // TODO Auto-generated method stub
                return emp.getId()>5;
            }

            
        });
        System.out.println("迭代后:");
        for(Emp emp:emps2){
            System.out.println(emp);
            
        }
        
    }
}
class Emp {
    public Emp(int id, String userName, int age) {
        this.id = id;
        this.userName = userName;
        this.age = age;
    }

    private String userName;
    private int id;
    private int age;

    public int getAge() {
        return age;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Emp [userName=" + userName + ", id=" + id + ", age=" + age + "]";
    }

}


运行结果:
Guava提供了可以在Iterator中进行处理的功能更丰富的迭代器, 其实就像是加了一个代理, 增加一些功能。
迭代前:
Emp [userName=Hello World!, id=0, age=10]
Emp [userName=Hello World!, id=1, age=11]
Emp [userName=Hello World!, id=2, age=12]
Emp [userName=Hello World!, id=3, age=13]
Emp [userName=Hello World!, id=4, age=14]
Emp [userName=Hello World!, id=5, age=15]
Emp [userName=Hello World!, id=6, age=16]
Emp [userName=Hello World!, id=7, age=17]
Emp [userName=Hello World!, id=8, age=18]
Emp [userName=Hello World!, id=9, age=19]
迭代后:
Emp [userName=Hello World!, id=6, age=16]
Emp [userName=Hello World!, id=7, age=17]
Emp [userName=Hello World!, id=8, age=18]
Emp [userName=Hello World!, id=9, age=19]

 

posted on 2018-11-10 15:21  Mature1021  阅读(756)  评论(0编辑  收藏  举报