1
stephenyin 2022-03-29 09:13:06 +08:00
正确使用 likely 和 unlikely 能够明显提升分支预测的准确率。
|
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"))); } |
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")); } |
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); } } |
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); } } |