lundi 6 mai 2019

java stream filter vs if-else condition, which one is faster?

Recently I started using JAVA-8 features, while implementing I have used stream filters. for my requirement I am filtering first and then updating in another loop. so I have iterated my loop twice. while in older way I could do the filtering and update on the same loop. So which one is better java stream filter & update or old way if-else and update?

sample code

private static void updateRank(List<DataDTO> dataList, Flag flag) {
    int rank = 1;
    List<DataDTO> updatedList = dataList.stream().filter(d -> flag.equals(d.getFlag())).sorted(Comparator.comparing(DataDTO::getUsdAmout).reversed()).collect(Collectors.toCollection(ArrayList::new));
    for (DataDTO data : updatedList) {
        data.setRank(data.getFlag().getTypeDesc() + " Rank " + rank++);
    }
}

Aucun commentaire:

Enregistrer un commentaire