1. 程式人生 > >java 開發技巧總結。自用

java 開發技巧總結。自用

        //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);