如何在java8中编写带有更多if / else条件的foreach?

时间:2018-01-08 15:05:08

标签: java java-8

我正在尝试使用一些foreach和多个if条件在java 8中开发一个方法。 如果我能为任何if条件写一个过滤器,我不知道吗?以及如何在java 8中做到这一点?

这是我的方法:

public Map<LocalDate,boolean[]> getForkedDetails() {
    TreeMap<LocalDate,boolean[]> map=new TreeMap<>();

        this.getDetails().forEach(dept->{
            boolean[] myArray=new boolean[3];
                        if(dept.getTypemt().equals(TypeMt.SIP)) {
                            myArray[0]=Boolean.TRUE;   
                        }
                        if(dept.getTypemt().equals(TypeMt.DOP)) {
                            myArray[1]=Boolean.TRUE;  
                        }
                        if(dept.getTypemt().equals(TypeMt.OPA) ) {
                            myArray[2]=Boolean.TRUE;                        
                        }

                        if(map.containsKey(dept.getDateFa())){
                            boolean[] bs = map.get(dept.getDateFa());
                            for(int index=0;index<bs.length;index++){
                                if(myArray[index]){
                                    bs[index]=myArray[index];
                                }
                            }
                            map.put(dept.getDateFa(), bs);
                        }else{                        
                            map.put(dept.getDateFa(), myArray);
                        }
        });   

        // get all dates between start and end dates
        List<LocalDate> dates = Stream.iterate(this.getDateDebut(), date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(this.getDateDebut(), this.getDateFin()))
                .collect(Collectors.toList()); 


        dates.forEach(date->{
            if(!map.containsKey(date)){
                map.put(date, new boolean[3]);
            }
        });

        // Sorted TreeMap
        TreeMap<LocalDate,boolean[]> result = map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, TreeMap::new));


    return result;
}

2 个答案:

答案 0 :(得分:2)

在进一步重新设计之前,有很多事情需要清理。首先,使用containsKey后跟getput创建巴洛克数组会带来几个不必要的地图查找。您可以改用merge。然后,无需将流收集到List,只需在其上应用forEach即可。您可以首先在流上使用forEach。好吧,TreeMap总是按键排序,执行流操作来排序它是没有意义的,只是收集到一个TreeMap,它将自己对条目进行排序。

public Map<LocalDate,boolean[]> getForkedDetails() {
    TreeMap<LocalDate,boolean[]> map=new TreeMap<>();

    this.getDetails().forEach(dept -> {
        boolean[] myArray= { dept.getTypemt().equals(TypeMt.SIP),
                             dept.getTypemt().equals(TypeMt.DOP),
                             dept.getTypemt().equals(TypeMt.OPA) };
        map.merge(dept.getDateFa(), myArray, (bs,newArray) -> {
            for(int index=0;index<bs.length;index++){
                if(newArray[index]) bs[index]=true;
            }
            return bs;
        });
    });

    // add entries for all missing dates between start and end dates
    Stream.iterate(this.getDateDebut(), date -> date.plusDays(1))
          .limit(ChronoUnit.DAYS.between(this.getDateDebut(), this.getDateFin()))
          .forEach(date-> map.computeIfAbsent(date, key -> new boolean[3]));

    // the TreeMap is already sorted
    return map;
}

然后,第一部分可以改写为

TreeMap<LocalDate,boolean[]> map = this.getDetails()
    .stream()
    .collect(Collectors.toMap(
        dept -> dept.getDateFa(),
        dept -> new boolean[] {  dept.getTypemt().equals(TypeMt.SIP),
                                 dept.getTypemt().equals(TypeMt.DOP),
                                 dept.getTypemt().equals(TypeMt.OPA) },
        (bs,newArray) -> {
            for(int index=0;index<bs.length;index++){
                if(newArray[index]) bs[index]=true;
            }
            return bs;
        },
        TreeMap::new));

答案 1 :(得分:0)

关闭Lino的评论应该是一种方法。而不是使用if-elseif-else的所有垃圾邮件,而只是做你的条件。它返回一个我假设的布尔值,所以你应该能够做到这一点。

你的回答基本上不是因为你不需要它们而使用它们。短而甜的代码是最好的代码(除非你不能理解代码高尔夫球手)