Java使用Session中的Hashmap,或者将其填入for循环

时间:2017-05-21 03:50:17

标签: java hashmap

我无法理解这一点。我的情况是:

  • 我从Session
  • 获取HashMap
  • 如果会话中的HashMap不为空,请在for循环
  • 中使用它
  • 否则,如果会话中的HashMap为空,请在for循环中填充

我的代码是这样的:

boolean isSubscriberToCtnMapEmpty = false;
Map<String, Type> subscriberWithType = new HashMap<String, Type>();
Map<String, String> subscriberToCtnMap = getMapFromSession(HttpServletRequest)

if (subscriberToCtnMap == null || subscriberToCtnMap.isEmpty()) {
     isSubscriberToCtnMapEmpty = true;
}

for (MobileNumber mobile : ListOfMobiles) {
     String subscriber = mobile.getSubscriberId();

     if (subscriber != null) {
          subscriberWithType.put(subscriber, Type.SUB_ID);
          if (isSubscriberToCtnMapEmpty)
               subscriberToCtnMap.put(subscriber, mobile.getMobileNumber());
     } else {
          subscriberWithType.put(mobile.getMobileNumber(), Type.MOBILE);
          //No need to put the entry in subscriberToCtnMap as subscriber is NULL
     }
}

//Set the subscriberToCtnMap in Session if not set already
if (isSubscriberToCtnMapEmpty) {
    session.setSubscriberToCtnMapping(subscriberToCtnMap);
}

如何提高上述逻辑的编码质量?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的测试public EarthquakeLoader(Context context, String url) { super(context); this.mUrl = url; } @Override public List<Earthquake> loadInBackground() { Log.e(LOG_TAG, "loadInBackGround()"); List<Earthquake> earthquakes = QueryUtils.extractEarthquakes(this.mUrl); return earthquakes; } @Override public void onStartLoading() { Log.e(LOG_TAG, "onStartLoading()"); forceLoad(); } @Override public void onStopLoading() { super.onStopLoading(); Log.e(LOG_TAG, "onStopLoading()"); } public void onReset() { super.onReset(); Log.e(LOG_TAG, "onReset()"); } subscriberToCtnMap`为空或空,完全相反。它们应该是这样的:

if (isSubscriberToCtnMapEmpty)...', no doubt intended to defend against the possibility of