1

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

这一切都归结为:如果我有几个 mapped 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如果至少有一个对象的值设置为错误的。

如果for(){if(){}}紧随其后if(){for(){}},我想知道是否有某种方法可以使其更高效和更易读?

4

2 回答 2

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);
}    
于 2013-03-19T15:47:31.160 回答
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;
} 
于 2013-03-19T15:58:19.700 回答