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(); } }
Java8 lambda example
猜你喜欢
转载自oywl2008.iteye.com/blog/2244926
今日推荐
周排行