简体   繁体   中英

how to write a foreach with more if/else conditions in java8?

I'am trying to develop a method in java 8 with some foreach and more than one if condition. I don't khnow if i can write for any if condition a filter? and how to do that in java 8?

Here is my methode:

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;
}

Before redesigning this further, there are a lot of things to clean up. First, the baroque array creation, then, using containsKey followed by get or put bears several unnecessary map lookups. You can use merge instead. Then, there is no need to collect a stream into a List , just to apply forEach on it. You can use forEach on the stream in the first place. Well, and TreeMap s are always sorted by key, there is no sense in performing a stream operation to sort it, just to collect into a TreeMap that will sort the entries by itself.

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;
}

Then, the first part can be rewritten as

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

Going off Lino's comment should be a way to go. Instead of all the spam with if-elseif-else just do what your condition is. It returns a boolean I assume so you should be able to just do that.

Your answer basically is don't use them because you don't need them. Short and sweet code is the best code (unless you then can't understand it code golfers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM