安卓篱笆

标题: 聚合操作例子(oracle甲骨文官网示例) [打印本页]

作者: GuiTarvvm2098    时间: 2018-1-4 09:34
标题: 聚合操作例子(oracle甲骨文官网示例)
import java.lang.Iterable;
import java.lang.Number;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class BulkDataOperationsExamples {
   
    public static void main(String... args) {
        
        // Create sample data

        List<Person> roster = Person.createRoster();
        
        // 1. Print names of members, for-each loop
        
        System.out.println("Members of the collection (for-each loop):");
        for (Person p : roster) {
            System.out.println(p.getName());
        }
        
        // 2. Print names of members, forEach operation
        
        System.out.println("Members of the collection (bulk data operations):");
        roster
            .stream()
            .forEach(e -> System.out.println(e.getName()));
            
        // 3. Print names of male members, forEach operation

        System.out.println(
            "Male members of the collection (bulk data operations):");
        roster
            .stream()
            .filter(e -> e.getGender() == Person.Sex.MALE)
            .forEach(e -> System.out.println(e.getName()));
            
        // 4. Print names of male members, for-each loop

        System.out.println("Male members of the collection (for-each loop):");
        for (Person p : roster) {
            if (p.getGender() == Person.Sex.MALE) {
                System.out.println(p.getName());
            }
        }
         
        // 5. Get average age of male members of the collection:
        
        double average = roster
            .stream()
            .filter(p -> p.getGender() == Person.Sex.MALE)
            .mapToInt(Person::getAge)
            .average()
            .getAsDouble();
            
        System.out.println(
            "Average age of male members (bulk data operations): " +
            average);
    }
}




欢迎光临 安卓篱笆 (http://www.okapk.cn/) Powered by Discuz! X3.2