Java8 lambda example

package com.generic;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
//import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {

	public static void main(String[] args) {
		
		Arrays.asList( "a", "b", "d" ).forEach( e -> System.out.println( e ) );

		Arrays.asList( "a", "b", "d" ).forEach( ( String e ) -> System.out.println( e ) );
		
		String separator = ",";
		
		Arrays.asList( "a", "b", "d" ).forEach(
		    ( String e ) -> System.out.print( e + separator ) );
		
		List<String> listStr=	Arrays.asList( "a","e", "b", "d" );
		listStr.sort( ( e1, e2 ) -> e1.compareTo( e2 ));
		
		System.out.println("");
		System.out.printf("List String: %s", listStr);
		System.out.println("");
		
		// Map reduce way
		// Old way:
		List<Integer> costBeforeTax1 = Arrays.asList(100, 200, 300, 400, 500);
		int avg= (int) costBeforeTax1.stream().mapToInt(x->x).summaryStatistics().getAverage();
		
		System.out.println("avg===="+ avg);
		
		double total = 0;
		for (Integer cost : costBeforeTax1) {
			double price = cost + .12*cost;
			total = total + price;
		}
		System.out.println("Total : " + total);
		// New way:
		List<Double> costBeforeTax = Arrays.asList(100.0, 200.0, 300.0, 400.0, 500.0);
		double bill = costBeforeTax.stream().filter(cost->cost>300).map((cost) -> cost + .12*cost).reduce((sum, cost) -> sum + cost).get();
		System.out.println("Total : " + bill);
		
		List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy","U.K.","Canada");
		String G7Countries = G7.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));
		System.out.println(G7Countries);
		
		//去掉重复记录
		List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
		List<Integer> distinct = numbers.stream().distinct().collect(Collectors.toList());
		System.out.printf("Original List : %s, Square Without duplicates : %s %n", numbers, distinct);
		
		// 取统计值
		List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
		IntSummaryStatistics stats = primes.stream().mapToInt(x -> x).summaryStatistics();
		System.out.println("Highest prime number in List : " + stats.getMax());
		System.out.println("Lowest prime number in List : " + stats.getMin());
		System.out.println("Sum of all prime numbers : " + stats.getSum());
		System.out.println("Average of all prime numbers : " + stats.getAverage());
		
		
		
		
		
		
		
		
		// TODO Auto-generated method stub
//		List<Integer> ints = new ArrayList<Integer>();
//		ints.add(1);
//		ints.add(2);
//		List<? extends Number> nums = ints; //compiler error
//		nums.get(1);
//		nums.add(3);
//		 System.out.print(ints); //output: [1, 2, 3.14]
		// Get the system clock as UTC offset
		final Clock clock = Clock.systemUTC();
		System.out.println( clock.instant() );
		System.out.println( clock.millis() );
		// Get the local date and local time
		final LocalDate date = LocalDate.now();
		final LocalDate dateFromClock = LocalDate.now( clock );

		System.out.println( date );
		System.out.println( dateFromClock );

		// Get the local date and local time
		final LocalTime time = LocalTime.now();
		final LocalTime timeFromClock = LocalTime.now( clock );

		System.out.println( time );
		System.out.println( timeFromClock );
		
		
		
		
		
		
		
		List<Set<String>> stringCollection = new ArrayList<>();
		Set<String> s = new HashSet<String>();  
        s.add("A");  
        s.add("B"); 
    	Set<String> s1 = new HashSet<String>();  
        s1.add("D");  
  
    	Set<String> s2 = new HashSet<String>();  
        s2.add("1");  
        s2.add("3");
        s2.add("5");

		stringCollection.add(s);
		stringCollection.add(s1);
		stringCollection.add(s2);
		
		
	System.out.println(	union(stringCollection));
		
		
		 

	}
	public static Set<String> union(List<Set<String>> sets){
		  return sets.stream().flatMap(s->getStrFromSets(s)).collect(Collectors.toSet());		
	}
	
	public static Stream<String> getStrFromSets(Set<String> set) {	
		String res=set.stream().reduce("",(a,b)->a+b+",");	
		return Arrays.asList(res.split(",")).stream();
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值