代码测试22

# DLP 代码测试

## 1. 测试代码

使用代码 java-learning-master 

## 2. 测试过程

1. 压缩包外发,将文件夹java-learning-master压缩并通过微信等工具外发。
2. 将下列内容粘贴至在线文档(印象笔记等)。

```java
package com.brianway.learning.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Created by brian on 17/2/28.
 * 构造函数引用
 */
public class ConstructorReference {

    public static void main(String[] args) {
        //Apple()的构造函数
        Supplier<Apple> c1 = Apple::new;
        Apple a1 = c1.get();
        System.out.println(a1);

        //Apple(int weight)的构造函数
        Function<Integer, Apple> c2 = Apple::new;
        Apple a2 = c2.apply(110);
        System.out.println(a2);

        List<Integer> weights = Arrays.asList(7, 3, 4, 10);
        List<Apple> apples = map(weights, Apple::new);
        System.out.println("list of weights:");
        apples.stream().forEach(System.out::println);

        //Apple(int weight, String color)的构造函数
        BiFunction<String, Integer, Apple> c3 = Apple::new;
        Apple a3 = c3.apply("green", 110);
        System.out.println(a3);
    }

    public static List<Apple> map(List<Integer> list,
                                  Function<Integer, Apple> f) {
        List<Apple> result = new ArrayList<>();
        //TODO
        for (Integer e : list) {
            result.add(f.apply(e));
        }
        return result;
    }

    public static class Apple {
        private int weight = 0;
        private String color = "";

        public String toString() {
            return "Apple{" +
                    "color='" + color + '\'' +
                    ", weight=" + weight +
                    '}';
        }
    }
}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值