Java 8使用流迭代大数据

时间:2019-03-15 19:44:03

标签: spring performance stream iteration

我的问题是关于10万条记录及其dateList的,迭代花费了很长时间。有什么办法可以在Java中更快地进行迭代?

public class TestIteration {

    public static void main(String[] args) {
        List<LocalDate> returnList = getDatesList(); 

        // `returnList` will have `[2018-03-14, 2018-03-15, 2018-03-16, 2018-03-17, 2018-03-18, 2018-03-19, 2018-03-20, 2018-03-21, ...]`

        Employee e = new Employee();
        for(int i=0;i<100000;i++) { 

            // parent loop that will run for 100k records

            for(LocalDate returnDate : returnList) { 

           // list of dates, need to iterate all dates and set in emp obj along with other setters

            e.setValue(i);
            e.setDate(returnDate);
            }
        }
    }

    private static List<LocalDate> getDatesList(){
        LocalDate fromDays = LocalDate.parse("2018-03-14");
        LocalDate toDays = LocalDate.parse("2018-12-24");

        String checkDays = "1234567";

        Long range = ChronoUnit.DAYS.between(fromDays, toDays); 
        System.out.println("range>>"+range);

        List<LocalDate> list = Stream.iterate(fromDays, d->d.plusDays(1)).limit(range+1)
                                .collect(Collectors.toList());
        System.out.println("dtlist>>"+list);
        String dayOfDate;
        List<LocalDate> dateList = new ArrayList();
        for(LocalDate d : list) {
            dayOfDate = String.valueOf(d.getDayOfWeek().getValue());
            if(checkDays.contains(dayOfDate)) {
                dateList.add(d);
            }
        }
        return dateList; 
    }
}

0 个答案:

没有答案