V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
bober0728
V2EX  ›  问与答

多重 if 判断怎么优化?排序规则:等级从高到低,相同等级比较拥有同等级的数量,所有等级数量相等最后以得分进行比较

  •  
  •   bober0728 · 2022-03-29 08:56:33 +08:00 · 993 次点击
    这是一个创建于 963 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if(map2.get("unitLevel") != null && map1.get("unitLevel") !=null) {
    int flag = compareLevel(map2.get("unitLevel").toString(), map1.get("unitLevel").toString());
    if(flag > 0) {return flag;}
    if(flag == 0) {
    int flag2 = compareLevelCount((Integer)map2.get("countAA"),(Integer)map1.get("countAA"));
    if(flag2 >0) {return flag2;}
    if(flag2 == 0){
    int flag3 = compareLevelCount((Integer)map2.get("countA"),(Integer)map1.get("countA"));
    if(flag3 >0) {return flag3;}
    if(flag3 == 0){
    int flag4 = compareLevelCount((Integer)map2.get("countB"),(Integer)map1.get("countB"));
    if(flag4 >0) {return flag4;}
    if(flag4 == 0){
    int flag5 = compareLevelCount((Integer)map2.get("countC"),(Integer)map1.get("countC"));
    if(flag5 >0) {return flag5;}
    if(flag5 == 0){
    int flag6 = compareLevelCount((Integer)map2.get("countD"),(Integer)map1.get("countD"));
    if(flag6 >0) {return flag6;}
    if(flag6 == 0){
    return Double.valueOf((Double) map2.get("unitScore")).compareTo(Double.valueOf((Double) map1.get("unitScore")));

    }else{
    return flag6;
    }

    }else{
    return flag5;
    }
    }else{
    return flag4;
    }
    }else{
    return flag3;
    }

    }else{
    return flag2;
    }
    } else {
    return flag;
    }

    }
    stephenyin
        1
    stephenyin  
       2022-03-29 09:13:06 +08:00
    正确使用 likely 和 unlikely 能够明显提升分支预测的准确率。
    sunjiayao
        2
    sunjiayao  
       2022-03-29 10:20:18 +08:00
    if (map2.get("unitLevel") != null && map1.get("unitLevel") != null) {
    int flag = compareLevel(map2.get("unitLevel").toString(), map1.get("unitLevel").toString());
    if (flag != 0) {
    return flag;
    }
    int flag2 = compareLevelCount((Integer) map2.get("countAA"), (Integer) map1.get("countAA"));
    if (flag2 != 0) {
    return flag2;
    }
    int flag3 = compareLevelCount((Integer) map2.get("countA"), (Integer) map1.get("countA"));
    if (flag3 != 0) {
    return flag3;
    }
    int flag4 = compareLevelCount((Integer) map2.get("countB"), (Integer) map1.get("countB"));
    if (flag4 != 0) {
    return flag4;
    }

    int flag5 = compareLevelCount((Integer) map2.get("countC"), (Integer) map1.get("countC"));
    if (flag5 != 0) {
    return flag5;
    }
    int flag6 = compareLevelCount((Integer) map2.get("countD"), (Integer) map1.get("countD"));
    if (flag6 != 0) {
    return flag6;
    }
    return Double.valueOf((Double) map2.get("unitScore")).compareTo(Double.valueOf((Double) map1
    .get("unitScore")));
    }
    zzfer
        3
    zzfer  
       2022-03-29 10:31:19 +08:00
    if(xxx != null){
    //业务代码
    }
    可以改为
    if(xxx == null){
    return;
    }
    //业务代码

    能跳出 if 包裹最好

    map.get("xxx")是否有规律,如果有的话 你的 compareLevelCount 可以使用递归优化,显得代码整洁。但要写好注释并保证没有死循环
    if (flag != 0) {
    return flag;
    }else{
    return compareLevelCount((Integer) map2.get("xxx"), (Integer) map1.get("xxx"));
    }
    xdchl
        4
    xdchl  
       2022-03-29 11:57:50 +08:00
    如上面朋友所说,看看 map 的 key 是否能遵从某种逻辑进行变换。可以的话,大体如下
    public {returnType} {yourMethodName} ({yourParameters}) {
    if (map2.get("unitLevel") == null || map1.get("unitLevel") == null) {
    // return something according to your business logic
    } else {
    recursiveCompareLevelCount("first key0", "first key1", map1, map2);
    }
    }

    private {returnType} recursiveCompareLevelCount(String key0, String key1, Map<K, V> map0, Map<K, V> map1) {
    // key0 and key1 equals your end compare logic
    if (key0.equals("Something") && key1.equals("Something")) {
    return Double.valueOf((Double) map1.get(key0)).compareTo(Double.valueOf((Double) map0.get(key1)));
    }

    int flag = compareLevelCount((Integer) map1.get(key1), (Integer) map0.get(key0));
    if (flag != 0) {
    return flag;
    } else {
    // set some rule to increment the key
    return compareLevelCount(new String("increment key0"), new String("increment key1"), map0, map1);
    }
    }
    xdchl
        5
    xdchl  
       2022-03-29 13:57:26 +08:00
    @xdchl 补充,主方法 else 少了个 return
    xdchl
        6
    xdchl  
       2022-03-29 14:01:25 +08:00
    @xdchl 最后 else 里面是调用 recursiveCompareLevelCount ,打错了
    xdchl
        7
    xdchl  
       2022-03-29 14:20:02 +08:00
    @xdchl 写完算了
    import java.util.HashMap;
    import java.util.Map;

    public class MutipleIfQuestionFromV2ex {
    public int testMethod() {
    Map<String, Integer> map1 = new HashMap<>();
    Map<String, Integer> map2 = new HashMap<>();
    if (map2.get("unitLevel") == null || map1.get("unitLevel") == null) {
    // return something according to your business logic
    return 0;
    } else {
    return recursiveCompareLevelCount("countAA", "countAA", map1, map2);
    }
    }

    private int recursiveCompareLevelCount(String key0, String key1, Map<String, Integer> map0, Map<String, Integer> map1) {
    // key0 and key1 equals your end compare logic
    if (key0.equals("unitScore") && key1.equals("unitScore")) {
    return map1.get(key0).compareTo(map0.get(key1));
    }

    int flag = compareLevelCount(map1.get(key1), map0.get(key0));
    if (flag != 0) {
    return flag;
    } else {
    // set some rule to increment the key
    char count = (char) (key0.charAt(key0.length() - 1) + 1);
    if (count == 'E') {
    key0 = "unitScore";
    key1 = "unitScore";
    } else if (count == 'B' && key0.charAt(key0.length() - 2) == 'A') {
    key0 = "countA";
    key1 = "countA";
    } else {
    key0 = key0.substring(0, key0.length() - 1) + count;
    key1 = key1.substring(0, key1.length() - 1) + count;
    }
    return recursiveCompareLevelCount(key0, key1 , map0, map1);
    }
    }

    private int compareLevelCount(int v1, int v2) {
    return Integer.compare(v1, v2);
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1119 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 19:07 · PVG 03:07 · LAX 11:07 · JFK 14:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.