Java HashMap 排序
可能的重复:
Java 有序映射
我在 HashMap
ProductName ProductCode Qty Price
Pen 100011 10 10.00 product1
Penci 100012 5 5.00 product2
HashMap<Integer,Product> productMap = new HashMap<Integer,Product>();
当用户单击 ProductName 、productCode 或 Price 时,对象应根据我的要求进行排序。
I added like this.
productMap .put(1,product1);
productMap .put(2,product2);
我该怎么做。我想使用对象排序。而不是键
请帮助我。
提前致谢
Possible Duplicate:
Java Ordered Map
I have list of product object in the HashMap<Integer,Product>
I want to do the sorting
ProductName ProductCode Qty Price
Pen 100011 10 10.00 product1
Penci 100012 5 5.00 product2
HashMap<Integer,Product> productMap = new HashMap<Integer,Product>();
When the user click on the ProductName ,productCode or Price, the object should sort according to the my requirements.
I added like this.
productMap .put(1,product1);
productMap .put(2,product2);
How can i do this.I want to sort using the object.not key
Please help me.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想频繁地基于键访问值,则不应该使用HashMap。
只需使用值列表并实现不同的
Comparator
即可。然后使用适当的比较器对列表进行排序。If you don't want to frequently access the values based on the key, you shouldn't use HashMap.
Just use a List of the values and implement different
Comparator<Product>
s. Then sort the list with the appropriate comparator.HashMap 没有排序,您可以使用 TreeMap如果您需要排序的地图。或者,您可以获取 keySet,对它进行排序,迭代它并从 HashMap 中提取信息,但恕我直言,这是不必要的。
HashMaps are not sorted, you can use TreeMap if you need a sorted map. Alternatively, you could get the keySet, sort it, iterate it and pull information from HashMap, but that would be unnecessary IMHO.
HashMap 映射 = new HashMap();
hash map以键值对的形式存储值。它不是同步的(意味着它可以作用于多个线程)。hasmap的初始容量为16,负载因子为0.75。
初始容量*负载系数=16*0.75=12
这意味着存储第 12 个键值对后,哈希映射大小将增加一倍。
当您使用它的键和值对时。这些值将以无序的方式使用。
您也可以尝试它(用于排序)----->
}
HashMap map = new HashMap();
hash map is storing the values in the form of key,value pair.and it was not synchronized(means it can act upon many threads).intial capacity of hasmap is 16.and load factor is 0.75.
intial capacity*loadfactor=16*0.75=12
that means after storing the 12 th key vale pair hash map size is doubled.
When you use its key and value pair.them these values will be used in unordered way..
You Can also try it(for sorting)------->
}