Loading

【java】使用java.util的【Collections】简化List创建

我们在创建一个List并往其中加入一个元素的时候一般会这么做:

    public List<User> getCurrentUser() {
        List<User> users = new ArrayList<>();
        User user = userService.getCurrentUser();
        users.add(user);
        return users;
    }

可以利用Collections来简化这个过程

    public List<User> getCurrentUser() {
        List<User> users = Collections.singletonList(userService.getCurrentUser());
        return users;
    }
posted @ 2019-10-13 21:31  yaro-feng  阅读(861)  评论(0编辑  收藏  举报