1. 程式人生 > >java8 Lambda表示式的學習與測試

java8 Lambda表示式的學習與測試

public class Employee {

    private String name;
    private String sex;
    private int age;

    public Employee(String name, String sex, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public String getSex() {
        return sex;
    }
    public int getAge() {
        return age;
    }
    
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Employee:[name=").append(name).append(", sex=").append(sex).append(", age=").append(age)
                .append("]");
        return builder.toString();
    }        
}
	@Test
	/**
	 * Stream測試
	 */
	public void StreamTEST() {
		
		List<Employee> employs = new ArrayList<>();
		
		employs.add(new Employee("張三", "男", 25));
		employs.add(new Employee("李四", "女", 24));
		employs.add(new Employee("王五", "女", 23));
		employs.add(new Employee("趙六", "男", 22));
		employs.add(new Employee("孫七", "女", 21));
		employs.add(new Employee("周八", "男", 20));
		employs.add(new Employee("吳九", "女", 19));
		employs.add(new Employee("鄭十", "男", 18));
		
		Consumer<Employee> printAction = System.out::println;
		
		//列印所有員工
		System.out.println("列印所有員工");
		employs.stream().forEach(printAction);
		//employs.forEach(printAction);
		
		//按年齡排序
		System.out.println("按照年齡排序");
		//Collections.sort(employs, (e1, e2)-> e1.getAge()-e2.getAge());
		//employs.forEach(printAction);
		employs.stream()
			.sorted((e1, e2) -> e1.getAge() - e2.getAge())
			.forEach(printAction);
		
		//列印年齡最大女工
		System.out.println("列印年齡最大女工");
		Employee maxAgeWoman = employs.stream()
				.filter(e -> "女".equals(e.getSex())).max((e1, e2) -> e1.getAge()-e2.getAge())
				.get();
		printAction.accept(maxAgeWoman);
		
		//列印年齡大於20的男員工
		System.out.println("列印所有年齡大於20的男員工");
		employs.stream().filter(e -> e.getAge() > 20 && "男".equals(e.getSex())).forEach(printAction);
		
		//列印年齡最大的2名員工
		System.out.println("列印所有年齡最大的兩名男員工");
		employs
			.stream()
			.filter(e -> "男".equals(e.getSex()))
			.sorted((e1, e2) -> e2.getAge()-e1.getAge())
			.limit(2)
			.forEach(printAction);
		
		//列印所有員工姓名,使用", "分割
		Collector<CharSequence, ?, String> collByComma =  Collectors.joining(", ");
		String s = employs.stream().map(Employee::getName).collect(collByComma);
		System.out.println("所有員工:" + s);
		
		//統計資訊
		IntSummaryStatistics summary = employs.stream().mapToInt(Employee::getAge).summaryStatistics();
		System.out.println("員工個數:" + summary.getCount());
		System.out.println("平均年齡:" + summary.getAverage());
		System.out.println("最大年齡:" + summary.getMax());
		System.out.println("最小年齡:" + summary.getMin());
		System.out.println("年齡總和:" + summary.getSum());
		
		//分組
		System.out.println("男士");
		Collector<Employee, ?, Map<String, List<Employee>>> collBySex = Collectors.groupingBy(Employee::getSex);
		Map<String, List<Employee>> map = employs.stream().collect(collBySex);
		map.get("男").forEach(printAction);
		System.out.println("女士");
		map.get("女").forEach(printAction);
		
		Collector<Employee, ?, Integer> totalAges = Collectors.summingInt(Employee::getAge);
		Collector<Employee, ?, Map<String, Integer>> totalAgesBySex = Collectors.groupingBy(Employee::getSex, totalAges);
		Map<String, Integer> totalBySex = employs.stream().collect(totalAgesBySex);
		System.out.println("男生年齡總和:" + totalBySex.get("男"));
		System.out.println("女生年齡總和:" + totalBySex.get("女"));
		
		
		Collector<Employee, ?, Map<Boolean, List<Employee>>> partByAgeSize = Collectors.partitioningBy(e -> e.getAge()> 20);
		Map<Boolean, List<Employee>> mapByAgeSize = employs.stream().collect(partByAgeSize);
		System.out.println("年齡大於20");
		mapByAgeSize.get(true).forEach(printAction);
		System.out.println("年齡小於等於20");
		mapByAgeSize.get(false).forEach(printAction);
		
		//
	}
	
詳細請看,Java Lambda 表示式學習筆記 http://bbs.jointforce.com/topic/20028 (出處: 解放號論壇)