java 板


LINE

※ 引述《TonyQ (骨头)》之铭言: : 与其要写HashMap然後再另外对EntrySet写Comparator做排序 : 倒不如用 TreeMap (差别只有implements SortedMap) 搭配Comparator : 效能上会好一点(特别是取多次的时候每次取都要sort一次 ,蛮糟的) : TreeMap(Comparator<? super K> c) : Constructs a new, empty map, sorted according to the given : comparator. 基於和平、爱与正义,昨晚在工作到快要秀逗时拿了这个问题来想了一下, 其实是还可以活用 Tree + HashMap 来达成要求的 不过 HashMap 不能拿来直接用. public Class HashMapWithTreeSet<K extends Comparable,V extends Comparable> extends HashMap { private TreeMap<K> sortedKeys = new TreeMap<K>(); public HashMapWithTreeSet() { super(); sortedKeys = new TreeMap<K>(new internalComparator<K>(this)); } ....... class internalComparator<K extends Comparable> implements Comparator<K> { HashMapWithTreeSet<K, ? extends Comparable> hash = null; public internalComparator(HashMapWithTreeSet<K, ? extends Comparable> hash) { this.hash = hash; } public int compare(K k1, K k2) { Object v1 = hash.get(k1), v2 = hash.get(k2); if (v1 == null && v2 == null) return 0; if (v1 == null) return -1; if (v2 == null) return 1; int result = hash.get(k1).compareTo(hash.get(k2)); if (result == 0) return k1.compareTo(k2); return result; } } } 大家应该就知道我想要干什麽了吧... 嘿嘿嘿... 嘿嘿... 接下来只要 override HashMap 的 put, remove,以及 keySet 就达成目标了 XD 这... 算是旁门左道吧 囧rz (Ver 1.1 : 改了一下 comparator, 不然在 add 时出现问题...) (Ver 1.2 : 工作中间再偷懒测试......) 把两个方法做一下测试... 方法 A : HashMapWithTreeSet 方法 B : 整个 entrySet 抽出用 Collections.sort 1) HashMap 为 HashMap<Integer, String> 2) 准备 1000 个不同长度的 String 3) Randomly 决定 Key 4) Randomly 从 1000 个 String 中抽出一个当 Value 5) Randomly 决定是 put 还是 remove, 把 <key,value> 放入 6) 重覆 (3) - (5) n 次 然後那个把整个 Entry set 抽出来 sort 的方法,每个测试在 (3) - (5) 之间平均重覆 15 次 结果是 A 大胜... 抽插次数 n 超过四万次之後,A 就永远都比 B 要快了.... 你非得用这麽猥亵的字眼不行吗 <(# ̄皿 ̄)╮☆(__ __||) 而且要注意的是,A 的那个方法是任何时候抽出来的 keySet 都是 sorted 的, 而不像 B 只有十五次... 当然若是真的只需要在最後结果有 sorted,那还是用整个抽出来 sort 的方法比较快 [... 唉我怎麽放着工作不做,在这里搞些有的没的呢...] (Ver 1.3 : 哈哈哈,我真无聊) 刚刚想到,如果把 Set<Map.Entry> 转成 Map.Entry[] 再用 Arrays.sort 会不会 快一点 真的会 @_@ HashMap hm = new HashMap<Integer, String>; ...... Comparator ec = new Comparator<Map.Entry>() { ...... } Set<Map.Entry> s = hm.entrySet(); Map.Entry[] me = new Map.Entry[s.size()]; s.toArray(me); Arrays.sort(me, ec); 不过也只能撑到抽插次数十万左右 (喂!) Sorting 次数仍维持 15 次  -- 《为了要得到真相,就要向原 PO 伸图》 那就是伸图魔人的没图没真相原则,那时我们坚信那就是逼逼死的真实 靠么,图咧? --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 147.8.130.225 ※ 编辑: superlubu 来自: 147.8.130.225 (11/21 10:07) ※ 编辑: superlubu 来自: 147.8.130.225 (11/21 10:11) ※ 编辑: superlubu 来自: 147.8.130.225 (11/21 15:19) ※ 编辑: superlubu 来自: 147.8.130.225 (11/21 15:25)
1F:推 kians:谢罗 11/25 14:46







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP