java 8 lamdba表达式示例

来源整理网络,亲自测试

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class PersonService {

    public static void main(String[] args) {

        //lamdba表达式的10个示例
        //1.生成线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("这个是java7生成线程的方法");
            }
        }).start();


        new Thread(() -> System.out.println("这个是java8生成线程的方法")).start();

        //对集合进行遍历
        // Java 8之前:
        List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        for (Object feature : features) {
            System.out.println(feature);
        }
        // Java 8之后:
        List features2 = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        features2.forEach(n -> System.out.println(n));
        // 使用Java 8的方法引用更方便,方法引用由::双冒号操作符标示,
        features2.forEach(System.out::println);


        List<String> list = Arrays.asList("java", "hadoop", "python", "php", "lucene");
        filter(list, s -> s.startsWith("j"));//java
        //        filter(list, s->true);//全部输出


        Predicate<String> condition1 = str -> str.startsWith("j");
        Predicate<String> condition2 = str -> str.endsWith("h");
        //and
        Predicate<String> andCondition = condition1.and(condition2);
        boolean result = andCondition.test("jsadh");
        System.out.println(result);//true

        //or
        Predicate<String> orCondition = condition1.or(condition2);
        result = orCondition.test("jasd");
        System.out.println(result);//true

        //negate,对判断条件取反
        Predicate<String> negate = condition1.negate();
        System.out.println(negate.test("aj"));//true

        //isEqual(了解,比较两个对象是否相等)
        result = Predicate.isEqual("as").test("aaa");
        System.out.println(result);



        // 不使用lambda表达式为每个订单加上12%的税
//        List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
//        for (Integer cost : costBeforeTax) {
//            double price = cost + .12*cost;
//            System.out.println(price);
//        }

        // 使用lambda表达式
        List<Integer> costBeforeTax2 = Arrays.asList(100, 200, 300, 400, 500);
        costBeforeTax2.stream().map((cost) -> cost + cost*.12).forEach(System.out::println);



        // 为每个订单加上12%的税
//        List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
//        double total = 0;
//        for (Integer cost : costBeforeTax) {
//            double price = cost + .12*cost;
//            total = total + price;
//        }
//        System.out.println("Total : " + total);

        // 新方法:
        List<Integer> costBeforeTax3 = Arrays.asList(100, 200, 300, 400, 500);
        double bill = costBeforeTax3.stream().map((cost) -> cost + .12*cost).reduce((sum, cost) -> sum + cost).get();
        System.out.println("Total : " + bill);


         // 创建一个字符串列表,每个字符串长度大于2
        List<String> strList = Arrays.asList("QW", "QWQEWQEWE", "2", "4444532", "23232455");
        List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
        System.out.printf("Original List : %s, filtered list : %s %n", strList, filtered);


        // 将字符串换成大写并用逗号链接起来
        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().map( i -> i*i).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());
    }


    public static void filter(List<String> list, Predicate<String> predicate) {
        list.forEach(t -> {
            if (predicate.test(t)) {
                System.out.println(t);
            }
        });
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值