• 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]
  • 相关阅读:
    https://developer.aliyun.com/ebook?utm_content=g_1000256420
    形容词在句子中的位置
    顺风水水
    https://www.youtube.com/watch?v=pVIywLXDuRo 有趣到流泪
    经济机器是怎样运行的 (时长30分钟) Ray Dalio
    https://codepen.io/deadzq-the-decoder/pen/qBqQYXg
    Vue 10 test
    vue day2
    codepen学Vue day2
    代理总结更新
  • 原文地址:https://www.cnblogs.com/mature1021/p/9939523.html
Copyright © 2020-2023  润新知