java 开发技巧总结。自用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32534855/article/details/84146825
        //1.获得日期
        LocalDate localDate = LocalDate.now();
        //输出 yyyy-mm-dd
        System.out.println(localDate.toString());
        //获取今天是今年的第几天
        System.out.println(localDate.getDayOfYear());
        //第几月
        System.out.println(localDate.getMonthValue());
        //第几天
        System.out.println(localDate.getDayOfWeek().getValue());

        //2.便利User
        List<User> mapList = new ArrayList<>();
        //添加数据
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId((int) (Math.random()*100));
            user.setName("name" + i);
            mapList.add(user);
        }

        //根据id升序排序
        mapList.sort((s1,s2)-> s1.getId().compareTo(s2.getId()));

        //获取user对象里面的id返回到indexList里面
        List<Integer> indexList = mapList.stream()
                .map(User::getId).collect(Collectors.toList());

        //copy对象
        List<User> newMapList = mapList.stream()
                .map(oldUser -> {
                    User newUser = new User();
                    BeanUtils.copyProperties(oldUser, newUser);
                    return newUser;
                }).collect(Collectors.toList());


        //数组转List
        List<String> arrayToList = Arrays.asList("1","3","2","3");

        List<String> delRepetList = new ArrayList();

        for (int i = 0; i < 5; i++) {
            delRepetList.add(String.valueOf(i/2));
        }

        //去重复
        Set s = new HashSet(delRepetList);
        delRepetList.clear();
        delRepetList.addAll(s);

猜你喜欢

转载自blog.csdn.net/qq_32534855/article/details/84146825