当前位置: 首页 > 工具软件 > λJSON > 使用案例 >

jdk1.8 λ表达式

潘辰龙
2023-12-01

很多匿名内部类比如开一个线程用Runnable的形式

		new Thread(() -> {
            //TODO
        }, "A线程");

        new Thread(new Runnable() {
            @Override
            public void run() {
                //TODO
            }
        }, "B线程");

口诀就是:拷贝小括号, 写死右箭头, 落地大括号
因为在Runnable中只有一个run方法所以可以直接()->

还有就是有的有参数的:
demo
1、声明一个就收http返回值的函数式接口:

@FunctionalInterface
public interface DataCallback<T> {
    public void onSuccess(T data);
}

2、发送请求:

 public static void loadFaceBookAdsData(DataCallback<List<FacebookAdsRepo>> callback, String time) {
        Map<String, Object> params = new HashMap<>(5);
        params.put("metrics", FACEBOOK_ADS_METRICE);
        params.put("access_token", FACEBOOK_ADS_ACCESS_TOKEN);
        params.put("breakdowns", "['app','country','platform','placement_name']");
        params.put("until", time);
        params.put("since", time);
        try {
            String reslut = HttpUtil.getInstance().get(FACEBOOK_ADS_URL, params);
            JSONObject json = JSON.parseObject(reslut);
            if (json.get("data") != null) {
                JSONArray jsonArray = JSON.parseArray(json.get("data").toString());
                JSONObject resultJson = JSON.parseObject(jsonArray.get(0).toString());
                if (resultJson.get("results") != null) {
                    List<FacebookAdsRepo> repo = JSON.parseArray(resultJson.get("results").toString(), FacebookAdsRepo.class);
                    if (CollectionUtils.isNotEmpty(repo)) {
                        callback.onSuccess(repo);
                    }
                }
            }
        } catch (IOException e) {
            log.error("loadFaceBookAdsData error!", e);
        }
    }

3、接收返回结果:
3.1 、原始接收方式:

public void saveFaceBookAdsData(String time) {
        faceBookAdsRepository.deleteByOccurTimeAndChannel(DateUtil.strOccurTimeToIn(time), "yunbugame@gmail.com");
        List<FaceBookAdsItem> faceBookAdsItemlist = new ArrayList<>();
        HttpProxy.loadFaceBookAdsData(new DataCallback<List<FacebookAdsRepo>>() {
            @Override
            public void onSuccess(List<FacebookAdsRepo> data) {
                for (FacebookAdsRepo repo : data) {
                    FaceBookAdsItem faceBookAdsItem = new FaceBookAdsItem(repo);
                    faceBookAdsItem.setChannel("yunbugame@gmail.com");
                    faceBookAdsItem.setPlatform("facebook");
                    faceBookAdsItem.setOccurTime(DateUtil.strOccurTimeToIn(time));
                    faceBookAdsItemlist.add(faceBookAdsItem);
                }
                List<FaceBookAdsItem> bookAdsItems = faceBookAdsItemlist.stream().filter(f -> !f.getSystem().equalsIgnoreCase("unknown")).collect(Collectors.toList());
                faceBookAdsRepository.save(bookAdsItems);
            }
        }, time);
    }

3.2、采用jdk1.8接收方式,这里和上面一样只是多了参数把参数放进小括号

 public void saveFaceBookAdsData(String time) {
        faceBookAdsRepository.deleteByOccurTimeAndChannel(DateUtil.strOccurTimeToIn(time), "yunbugame@gmail.com");
        List<FaceBookAdsItem> faceBookAdsItemlist = new ArrayList<>();
        HttpProxy.loadFaceBookAdsData((List<FacebookAdsRepo> dataCallBack) -> {
            List<FacebookAdsRepo> data = dataCallBack;
            for (FacebookAdsRepo repo : data) {
                FaceBookAdsItem faceBookAdsItem = new FaceBookAdsItem(repo);
                faceBookAdsItem.setChannel("yunbugame@gmail.com");
                faceBookAdsItem.setPlatform("facebook");
                faceBookAdsItem.setOccurTime(DateUtil.strOccurTimeToIn(time));
                faceBookAdsItemlist.add(faceBookAdsItem);
            }
            List<FaceBookAdsItem> bookAdsItems = faceBookAdsItemlist.stream().filter(f -> !f.getSystem().equalsIgnoreCase("unknown")).collect(Collectors.toList());
            faceBookAdsRepository.save(bookAdsItems);
        }, time);
    }
 类似资料: