JAVA中 forEach 方法用法 及 stream.map用法

forEach方式可以通过java.util.function包中的相关接口进行处理

如:

package com.util.test;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class Testss {

    public static void main(String[] args) {
    	
    	
        ArrayList employees = new ArrayList<>();
        String[] prefix = {"A", "B"};
        for (int i = 1; i <= 10; i++)
            employees.add(new Employee(prefix[i % 2] + i, i * 1000));
        for(Employee e : employees) {
        	System.out.println(e.getName()+"--"+e.getSalary());
        }
        employees.forEach(a->System.out.println(a));
        
        Supplier t = NameConsumer::new;
        Supplier t1 = SalaryConsumer::new;
        employees.forEach(t.get());
        employees.forEach(t1.get());
    }

    static class Employee {
        private String name;
        private int salary;

        public Employee() {
            this.salary = 4000;
        }

        public Employee(String name, int salary) {
            this.name = name;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        @Override
        public String toString() {
            return new StringBuilder()
                    .append("name:").append(name)
                    .append(",salary:").append(salary)
                    .toString();
        }
    }
    
    // 输出需要交税的员工
    static class SalaryConsumer implements Consumer {

        @Override
        public void accept(Employee employee) {
            if (employee.getSalary() > 2000) {
                System.out.println(employee.getName() + "要交税了.");
            }
        }
        
    }
    
    // 输出需要名字前缀是‘A’的员工信息
    static class NameConsumer implements Consumer {

        @Override
        public void accept(Employee employee) {
            if (employee.getName().startsWith("A")) {
                System.out.println(employee.getName() + " salary is " + employee.getSalary());
            }
        }

    }
}

代码输出为:

B1--1000
A2--2000
B3--3000
A4--4000
B5--5000
A6--6000
B7--7000
A8--8000
B9--9000
A10--10000
name:B1,salary:1000
name:A2,salary:2000
name:B3,salary:3000
name:A4,salary:4000
name:B5,salary:5000
name:A6,salary:6000
name:B7,salary:7000
name:A8,salary:8000
name:B9,salary:9000
name:A10,salary:10000
A2 salary is 2000
A4 salary is 4000
A6 salary is 6000
A8 salary is 8000
A10 salary is 10000
B3要交税了.
A4要交税了.
B5要交税了.
A6要交税了.
B7要交税了.
A8要交税了.
B9要交税了.
A10要交税了.

 stream.map用于对数据做二次处理

如以下代码-统计有几个可以被2带除的数字

	public static void main(String[] args) {
		
		List num = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
		
		System.out.println(num.stream().map(a->a%2 == 0 ? 1 : 0).reduce((a,b)->a+b).get());
		
	}

代码中通过map判断是否能被2整除,然后通过reduce进行统计

你可能感兴趣的:(JAVA)