2017-05-21 17 views
0

これを理解することができません。JavaセッションからのHashmapを使用するか、forループで使用してください。

  • 私はセッション
  • からHashMapを取得するセッションからのHashMapが空でない場合は、セッションからの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); 
} 

どのように私は、Aの符号化品質を向上させることができます論理をボブ?

ありがとうございます!

+0

1つの提案は、ライン IF(isSubscriberToCtnMapEmpty) subscriberToCtnMap.put(加入者、mobile.getMobileNumber())のためであることができます。 isSubscriberToCtnMapEmptyをtrueに設定しているときは、リストがnullか空であるかどうかの条件をチェックします。リストがnullの場合、変数もtrueに設定されます。あなたのコードでは、ブール値をチェックしてマップに値を代入するだけで、マップがnullの場合、代入コードは失敗します。マップがnullでなく、空でない場合は発生しません。 –

+0

私はgetMapFromSession(HttpServletRequest)にそれを追加するのを忘れていました。私の最初の行は次のとおりです:Map map = new HashMap () - セッション値がnullであっても、応答。だから私の "put"コールはいつも空のMap(getMapFromSession()コールで宣言されているのでnullにはなりません) –

答えて

0

あなたのテストif (isSubscriberToCtnMapEmpty)...', no doubt intended to defend against the possibility of subscriberToCtnMapはヌルまたは空です。正反対です。彼らはこのようにすべきである:

if (!isSubscriberToCtnMapEmpty) { // added the ! (not) operator 
    // only do this if if the map is NOT empty 
} 
関連する問題