时间:2015-12-24 11:11:08

标签: java indexing java-8 java-stream

我有一个流。我想计算流中的索引(我需​​要一些带有错误签名的行“сумм”)。 我想验证LinkedList中的索引数量。   我的代码:

    Map<String, Map<String, Map<String, Map<String, Map<String, List<RosterRow>>>>>> rost = roster.getRows().stream()
            .collect(Collectors.groupingBy(RosterRow::getBeneficiarySurname,
                    Collectors.groupingBy(RosterRow::getBeneficiaryName,
                            Collectors.groupingBy(RosterRow::getBeneficiaryPatronymic,
                                    Collectors.groupingBy(RosterRow::getBeneficiaryIdentificationDocumentSerial,
                                            Collectors.groupingBy(RosterRow::getBeneficiaryIdentificationDocumentNumber)
                                    )))));
    rost.entrySet().stream()
            .map(entry -> rost.get(entry.getKey()))
            .map(each -> each.entrySet())
            .flatMap(Collection::stream)
            .map(entry -> entry.getValue())
            .map(each -> each.entrySet())
            .flatMap(Collection::stream)
            .map(entry -> entry.getValue())
            .map(each -> each.entrySet())
            .flatMap(Collection::stream)
            .map(entry -> entry.getValue())
            .map(each -> each.entrySet())
            .flatMap(Collection::stream)
            .map(entry -> entry.getValue())

            .forEach(group -> {    
                List<String> indexes = group.stream()
                        .filter(row -> row.getComment().contains("сумма"))
                        .map(RosterRow::getComment)
                        .map(s -> s.substring(s.length() - 1))
                        .collect(Collectors.toList());
                int numberofrows = group.size();                
                List <String> places = new ArrayList<>();
                for (int i = 1; i < numberofrows + 1; i++) {
                    places.add(Integer.toString(i));
                };
                if (!indexes.containsAll(places)) {
                    throw new InvalidAccrualException(String.format("%s: %s", bundle.getString("Invalid_field_value"), "NOTE"));
                }
            });
    return Collections.EMPTY_LIST;
}  

1 个答案:

答案 0 :(得分:2)

要获得您可以写的行数:

List<RosterRow> rows = roster.getRows();
OptionalInt index = IntStream.range(0, rows.size())
         .filter(idx -> rows.get(idx).getComment().contains("сумма"))
         .findFirst();