Java从会话中使用Hashmap,或者将其填充到for循环中
我很难理解这一点。我的设想是:Java从会话中使用Hashmap,或者将其填充到for循环中,java,hashmap,Java,Hashmap,我很难理解这一点。我的设想是: 我从会话中获取HashMap 如果来自会话的HashMap不是空的,则在for循环中使用它 否则,如果来自会话的HashMap为空,则将其填充到for循环中 我的代码如下: boolean isSubscriberToCtnMapEmpty = false; Map<String, Type> subscriberWithType = new HashMap<String, Type>(); Map<String, String&
- 我从会话中获取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);
}
if (!isSubscriberToCtnMapEmpty) { // added the ! (not) operator
// only do this if if the map is NOT empty
}
布尔isSubscriberToCtnMapEmpty=false;
Map subscriberWithType=new HashMap();
Map subscriberToCtnMap=getMapFromSession(HttpServletRequest)
if(subscriberToCtnMap==null | | subscriberToCtnMap.isEmpty()){
isSubscriberToCtnMapEmpty=真;
}
for(移动电话号码:ListOfMobiles){
字符串subscriber=mobile.getSubscriberId();
if(订户!=null){
subscriberWithType.put(订阅者,Type.SUB_ID);
如果(IssuBScriberToCtnapempty)
subscriberOctNmap.put(subscriber,mobile.getMobileNumber());
}否则{
subscriberWithType.put(mobile.getMobileNumber(),Type.mobile);
//由于订阅服务器为空,无需将条目放入SubscriberOctNMap
}
}
//如果尚未设置,请在会话中设置SubscriberOctNMap
如果(IssuBScriberToCtnapempty){
session.setSubscriberOctNMap(SubscriberOctNMap);
}
如何提高上述逻辑的编码质量
谢谢 您的测试如果(issubscribertoctnapempty)…..,无疑是为了防范
subscriberToCtnMap`为null或空的可能性,请执行完全相反的操作。它们应该是这样的:
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);
}
if (!isSubscriberToCtnMapEmpty) { // added the ! (not) operator
// only do this if if the map is NOT empty
}
如果(isSubscriberToCtnMapEmpty)subscriberOctnmap.put(subscriber,mobile.getMobileNumber()),则可以为该行提供一个建议;当您将isSubscriberToCtnMapEmpty设置为true时,检查两个条件,即列表是否为空。假设列表为null,那么变量也会设置为true。现在在您的代码中,您只需检查布尔值并将值赋值给映射,现在考虑MAP为NULL,那么您的赋值代码将失败,如果map不是NULL并且是空的,那么它不会发生。我忘了在GETMAPFROM会话(HOPTServServices请求)中添加,我的第一行是:MAPMAP =新HashMap()。-因此,即使会话值为null,我也会得到一个声明的映射作为响应。因此,我的“put”调用将始终发生在空映射上(映射永远不会为null,因为它是在getMapFromSession()调用中声明的)