java - HashMap<> 和 if else一起使用的问题.
PHP中文网
PHP中文网 2017-04-18 10:24:15
0
3
425
/**
 * 对 界面上显示的娱乐版块进行排序.(优先级)
 *
 * @return
 */
private  BaseMessage entertainmentSort() {
    int size = mEntertainmentMap.size();
    BaseMessage baseMessage = null;
    if (size > 0) {
        if (mEntertainmentMap.containsKey(TypeCst.LiveEntertainmentType.LIVE_TYPE_AUCTION)) {
            baseMessage = mEntertainmentMap.get(TypeCst.LiveEntertainmentType.LIVE_TYPE_AUCTION);
        } else if (mEntertainmentMap.containsKey(TypeCst.LiveEntertainmentType.LIVE_TYPE_GUESS)) {
            baseMessage = mEntertainmentMap.get(TypeCst.LiveEntertainmentType.LIVE_TYPE_GUESS);
        } else if (mEntertainmentMap.containsKey(TypeCst.LiveEntertainmentType.LIVE_TYPE_BET_VS)) {
            baseMessage = mEntertainmentMap.get(TypeCst.LiveEntertainmentType.LIVE_TYPE_BET_VS);
        } else if (mEntertainmentMap.containsKey(TypeCst.LiveEntertainmentType.LIVE_TYPE_BET_CONTENT)) {
            baseMessage = mEntertainmentMap.get(TypeCst.LiveEntertainmentType.LIVE_TYPE_BET_CONTENT);
        } else {
            baseMessage = null;
        }
    } else {
        baseMessage = null;
    }
    return baseMessage;
}


先帖一波代码,这样运行HashMap 为什么 会出现 打断点 已经进入else if 然后又返回到另一个else if 什么鬼....

tips: 开始以为是同步 或者线程锁的问题,但是更换HashTable和其他并发 同步 容器 ,问题还没有解决 ..

求大神 相助,我仔细检查代码 是没问题的啊.

详细描述下问题:怕大家看不懂,就是 已经执行第三个else if ,下一步 直接返回到第二个else if.

我把每个else if都打印了线程ID ,没发现子线程 也枷锁了 .

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
Peter_Zhu

I don’t see any big problem, but the questioner could consider the coding style. Might help Debug

private  BaseMessage entertainmentSort() {
    if (!mEnterainmerntMap.isEmpty()) {
        TypeCst.LiveEntertaimmentType[] types = new TypeCst.LiveEntertainmentType[]{TYPE_ACTION, TYPE_GUESS, TYPE_BET_VS, TYPE_BET_CONTENT};
        for (TypeCst.LiveENtertainmentType type : types) {
            if (mEnterainmerntMap.containsKey(type))
                return mEnterainmerntMap.get(type);
        }        
    }
    return null;
}
洪涛

Clean the project and try restarting eclipse

PHPzhong

Thank you everyone, the problem has been solved. It was the IDE that deceived me. The JDK syntax itself does not have Return and then Return. This situation may occur in the Debug mode on Android Studio.

There is no multi-threading either. In short, don’t be blinded by this problem. Hit more breakpoints.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!