简体   繁体   English

如何避免嵌套for-each循环?

[英]How to avoid nested for-each loops?

I've got a class which should parse an XML file, like this: 我有一个应该解析XML文件的类,如下所示:

<customers>
        ...
        <customer>
            <id>123456</id>
            <name>Mike</name>
            <orders>
                ...
                <order>
                    <id>233658</id>
                    <positions>
                        ...
                        <position>
                            <id>12345</id>
                            <price>10.0</price>
                            <count>5</count>
                        </position>
                        ...
                    </positions>
                </order>
                ...
            </orders>
        </customer>
<customers>

I'm going to unmarshall it with JAXB and than process result objects to get statistics (like max order amount, total orders amount etc) 我将使用JAXB解组它并处理结果对象以获取统计信息(如最大订单金额,总订单金额等)

Is this a bad practice to use 3-level foreach loop in this case? 在这种情况下,使用3级foreach循环是不好的做法吗?

public void getStatistics() {
    for (Customer customer: this.customers.getCustomer()) {

        BigDecimal customerTotalAmount = new BigDecimal(0);
        for (Order order : customer.getOrders().getOrder()) {

            BigDecimal orderAmount = new BigDecimal(0);
            for (Position position : order.getPositions().getPosition()) {
                orderAmount = orderAmount.add( position.getPrice().multiply(new BigDecimal(position.getCount())) );
            }

            customerTotalAmount = customerTotalAmount.add(orderAmount);
            this.totalOrders++;
        }

        this.totalAmount = this.totalAmount.add(customerTotalAmount);
    }
}

Customer, Order and Position classes has been generated automatically from XSD schema, and i think it is not good to change them. 客户,订单和职位类已从XSD架构自动生成,我认为更改它们并不好。

What am i doing wrong? 我究竟做错了什么? How can i avoid those nested loops? 我怎样才能避免那些嵌套循环?

Thank you. 谢谢。

I would recommend extracting some methods: 我建议提取一些方法:

public void getStatistics() {
    for (Customer customer: this.customers.getCustomer()) {
        BigDecimal customerTotalAmount = processCustomer(customer);
        this.totalAmount = this.totalAmount.add(customerTotalAmount);
    }
}

private void processCustomer(Customer customer){
    BigDecimal customerTotalAmount = new BigDecimal(0);
    for (Order order : customer.getOrders().getOrder()) {
        BigDecimal orderAmount = new BigDecimal(0);
        for (Position position : order.getPositions().getPosition()) {
            orderAmount = orderAmount.add( position.getPrice().multiply(new BigDecimal(position.getCount())) );
        }

        customerTotalAmount = customerTotalAmount.add(orderAmount);
        this.totalOrders++;
    }
    return customerTotalAmount;
}

Do the same thing for the Order and Position loop, give the methods descriptive-enough names and make sure they return proper values, and you get a nice, clean code. 对Order和Position循环执行相同的操作,为方法提供足够的描述名称,并确保它们返回正确的值,并获得一个漂亮,干净的代码。 These are still nested loops, but at least your eyes don't hurt when you see them. 这些仍然是嵌套的循环,但是当你看到它们时,至少你的眼睛不会受伤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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