Java HashMap 排序

发布于 2024-12-14 00:10:11 字数 728 浏览 1 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万水千山粽是情ミ 2024-12-21 00:10:11

如果您不想频繁地基于键访问值,则不应该使用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.

暮年慕年 2024-12-21 00:10:11

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.

ゞ花落谁相伴 2024-12-21 00:10:11

HashMap 映射 = new HashMap();

hash map以键值对的形式存储值。它不是同步的(意味着它可以作用于多个线程)。hasmap的初始容量为16,负载因子为0.75。
初始容量*负载系数=16*0.75=12
这意味着存储第 12 个键值对后,哈希映射大小将增加一倍。

当您使用它的键和值对时。这些值将以无序的方式使用。

您也可以尝试它(用于排序)----->

public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);

LinkedHashMap sortedMap = 
    new LinkedHashMap();

Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
    Object val = valueIt.next();
    Iterator keyIt = mapKeys.iterator();

    while (keyIt.hasNext()) {
        Object key = keyIt.next();
        String comp1 = passedMap.get(key).toString();
        String comp2 = val.toString();

        if (comp1.equals(comp2)){
            passedMap.remove(key);
            mapKeys.remove(key);
            sortedMap.put((String)key, (Double)val);
            break;
        }

    }

}
return sortedMap;

}

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)------->

public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);

LinkedHashMap sortedMap = 
    new LinkedHashMap();

Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
    Object val = valueIt.next();
    Iterator keyIt = mapKeys.iterator();

    while (keyIt.hasNext()) {
        Object key = keyIt.next();
        String comp1 = passedMap.get(key).toString();
        String comp2 = val.toString();

        if (comp1.equals(comp2)){
            passedMap.remove(key);
            mapKeys.remove(key);
            sortedMap.put((String)key, (Double)val);
            break;
        }

    }

}
return sortedMap;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文