Używam java 8. Mam klasę Operator, która ma 3 pola. class Operator{ private String typ; Żądanie prywatnej aktualizacji logicznej; prywatna wartość logiczna deleteRequested; } Mam listę...

0
user5669842 24 czerwiec 2021, 18:59

4 odpowiedzi

Najlepsza odpowiedź

Oto implementacja przy użyciu Kolektory.doMapy

class Operator {
    public String type;
    public boolean updateRequested;
    public boolean deleteRequested;
    Operator(String type, boolean updateRequested, boolean deleteRequested) {
        this.type = type;
        this.updateRequested = updateRequested;
        this.deleteRequested = deleteRequested;
    }
}

class Result {
    public int deleteReqCount;
    public int updateReqCount;
    Result(int deleteReqCount, int updateReqCount) {
        this.deleteReqCount = deleteReqCount;
        this.updateReqCount = updateReqCount;
    }

    @Override
    public String toString() {
        return "Result{" +
                "deleteReqCount=" + deleteReqCount +
                ", updateReqCount=" + updateReqCount +
                '}';
    }
}

Map<String, Result> solve(List<Operator> operatorList) {
    return operatorList.stream()
            .collect(Collectors.toMap(
                    v -> v.type,
                    v -> new Result(v.deleteRequested ? 1 : 0, v.updateRequested ? 1 : 0),
                    (result, result2) -> {
                        int deleteReqCount = result.deleteReqCount + result2.deleteReqCount;
                        int updateReqCount = result.updateReqCount + result2.updateReqCount;
                        return new Result(deleteReqCount, updateReqCount);
                    }
            ));
}

List<Operator> operatorList = Arrays.asList(
        new Operator("cricket", true, true),
        new Operator("cricket", true, false),
        new Operator("cricket", true, true),
        new Operator("soccer", false, true),
        new Operator("soccer", true, true)
);
System.out.println(solve(operatorList));

Wynik:

{soccer=Result{deleteReqCount=2, updateReqCount=1}, cricket=Result{deleteReqCount=2, updateReqCount=3}}

2
Sai Kiran 24 czerwiec 2021, 16:27

Na czas aktualizacji do wersji Java 12 lub nowszej zadanie można wykonać za pomocą Collectors.teeing :

List<Operator> ops = //your operators list

Map<String, Result> myMap =          
    ops.stream()
       .collect(
            Collectors.groupingBy(Operator::getType,
                 Collectors.teeing(
                           Collectors.filtering(Operator::isUpdateRequested,Collectors.counting()),
                           Collectors.filtering(Operator::isDeleteRequested,Collectors.counting()),
                           (updateCount, deleteCount) -> {
                                return new Result(updateCount.intValue(),deleteCount.intValue());
                           }
               )));
0
Eritrean 24 czerwiec 2021, 16:45

Zgodnie z komentarzem @Andreas,

    operators.stream().collect(Collectors.toMap(
            Operator::getType,
            v -> new Result(v.isDeleteRequested() ? 1 : 0, v.isUpdateRequested() ? 1 : 0),
            Result::merge
    ));

Metoda łączenia,

  public Result merge(Result result) {
      this.updateReqCount+=result.updateReqCount;
      this.deleteReqCount+=result.deleteReqCount;
      
      return this;
  }
1
Vikas Yadav 24 czerwiec 2021, 17:16

Aktualizacja doskonałej odpowiedzi Sai Kirana:

Map<String, Result> solve(List<Operator> operatorList) {
    return operatorList.stream()
        .collect(Collectors.toMap(
                v -> v.type,
                v -> new Result(v.deleteRequested ? 1 : 0, v.updateRequested ? 1 : 0),
                (result, result2) -> {
                    result.updateReqCount += result2.updateReqCount;
                    result.deleteReqCount += result2.deleteReqCount;
                    return result;
                }
        ));
}
1
K.Nicholas 24 czerwiec 2021, 16:38