使用多个循环优化方法

时间:2013-03-19 15:30:42

标签: java loops optimization

我有一个包含多个错综复杂的forif语句的方法。资源使用并不重要,因为应用程序仍在开发中,但我想知道是否有某种方法可以优化它,因为它看起来非常重。

这一切都归结为:如果我有多个映射Object,有没有办法检查我的对象的一个​​(已知)字段中的值,如果满足该值,则更新所有这些值具有相同的值,而不会在我的地图上迭代太多次。

这是代码

private Map<Integer, Reservation> CreerMapFax(HttpServletRequest request, HttpSession session)
{
    // Get the "keys" parameter array from the request
    // and makes an array of integers
    String[] strKeys = request.getParameterValues("keys");
    Integer[] intKeys = new Integer[strKeys.length];
    for (int i = 0; i < strKeys.length; i++)
        intKeys[i] = Integer.parseInt(strKeys[i]);

    // Creates Map (1) of the selected bookings that need to be faxed,
    // gets Map (2) of all registered bookings within session
    // and fill (1) with data from (2)
    // by using the keys stored in the array
    boolean mail = true;

    Map<Integer, Booking> mapFax = new HashMap<Integer, Booking>();
    Map<Integer, Booking> bookings=
            (HashMap<Integer, Booking>) session.getAttribute(SESSION_BOOKINGS);

    for (int i = 0; i < intKeys.length; i++)
    {
        Booking booking = bookingss.get(intKeys[i]);
        if (!booking.getMailing())
            mail = false;

        // Some updating done here on "booking"
        ...

        // Overwrite old map values with new ones
        bookings.put(intKeys[i], booking);
        mapFax.put(intKeys[i], booking);

    }

    // mail == false whenever at least one of the booking
    // stored in the map had their getMailing() method return false

    if (!mail)
    {
        for (int j = 0; j < intKeys.length; j++)
        {
            // Updates AGAIN !
            Booking booking = mapFax.get(intKeys[j]);
            booking.setMailing(false);
            mapFax.put(intKeys[j], booking);
            reservations.put(intKeys[j], booking);
        }
    }

    session.setAttribute(SESSION_BOOKINGS, BOOKINGS);
    return mapFax;
} 

基本上,这样做的目的(除了更新Booking对象的其他字段并返回mapFax地图以进行进一步处理)是为每一个设置邮件字段为false mapFax中的对象,如果至少有一个值设置为false。

如果for(){if(){}}后跟if(){for(){}}并且我想知道是否有某种方法可以提高效率和可读性,那么会有什么问题?

2 个答案:

答案 0 :(得分:1)

我首先会查找邮件标志(一旦找到就打破循环),然后在一个循环中完成所有更新......

像这样......

...
for (int i = 0; i < intKeys.length; i++)
{
    Booking booking = bookingss.get(intKeys[i]);
    if (!booking.getMailing())
    {
        mail = false;
        break;
     }
}
....
for (int i = 0; i < intKeys.length; i++)
{
    // Some updating done here on "booking"
    ...     
    if (!mail)
    {
        ...
    }
    ...
  // Overwrite old map values with new ones
    bookings.put(intKeys[i], booking);
    mapFax.put(intKeys[i], booking);
}    

答案 1 :(得分:0)

private Map<Integer, Reservation> CreerMapFax(HttpServletRequest request, HttpSession session) {
    String[] strKeys = request.getParameterValues("keys");
    Integer[] intKeys = new Integer[strKeys.length];
    for (int i = 0; i < strKeys.length; i++) {
        intKeys[i] = Integer.parseInt(strKeys[i]);
    }

    boolean mail = true;

    Map<Integer, Booking> mapFax = new HashMap<Integer, Booking>();
    Map<Integer, Booking> bookings= (HashMap<Integer, Booking>) session.getAttribute(SESSION_BOOKINGS);

    for (Integer intKey : intKeys) {
        Booking booking = bookingss.get(intKey);
        if (!booking.getMailing()) {
            mail = false;
            break;
        }
    }

    for(Integer intKey : intKeys) {
        Booking booking = mapFax.get(intKey);
        mapFax.put(intKey, booking);
        if(!mail) {
            booking.setMailing(false);
            reservations.put(intKey, booking);
        }
    }

    session.setAttribute(SESSION_BOOKINGS, BOOKINGS);
    return mapFax;
}