在 Java 中:获取按映射键排序的映射值数组

发布于 2024-10-01 05:15:28 字数 375 浏览 0 评论 0原文

我有一个哈希映射,我希望获取此哈希映射中的值的数组,但我希望该数组按哈希映射中的键进行排序。

例如,如果地图如下所示:

  • <2,obj1>
  • <4,obj2>
  • <0,obj3>
  • <10,obj4>
  • <5,obj5>
  • <1,obj6>
  • <15,obj7>
  • <3,obj8>

我希望数组为: [obj3,obj6,obj1,obj8,obj2,obj5,obj4,obj7]

越快越好。有内置的方法可以做到这一点吗?

I have a hashmap, I wish to get an array of the values in this hashmap, but I wish for the array to be sorted by the keys in the hashmap.

For instance if the map looks like this:

  • <2,obj1>
  • <4,obj2>
  • <0,obj3>
  • <10,obj4>
  • <5,obj5>
  • <1,obj6>
  • <15,obj7>
  • <3,obj8>

I want the array to be: [obj3,obj6,obj1,obj8,obj2,obj5,obj4,obj7]

The faster the better. Is there a built in way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不疑不惑不回忆 2024-10-08 05:15:28

听起来您想要一个 SortedMap,特别是 TreeMap。您可以利用映射维护键的排序列表来生成输出数组,例如通过使用 SortedMap.values() 或迭代映射中的条目。

Sounds like you want a SortedMap, specifically a TreeMap. You can use the fact that the map maintains a sorted list of keys to generate your output array, e.g. by using SortedMap.values() or by iterating over the entries in the map.

另类 2024-10-08 05:15:28

我总是喜欢能够以通过单元测试的形式回答:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

public class SortedMapValuesTest extends TestCase {
    public void testSortedMapValues() throws Exception {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1, 200);
        map.put(2, 300);
        map.put(3, 100);
        List<Integer> list = getSortedMapValues(map);
        assertEquals("[200, 300, 100]", list.toString());
    }

    private <K extends Comparable<K>, V> List<V> getSortedMapValues(Map<K, V> map) {
        ArrayList<K> keys = new ArrayList<K>(map.keySet());
        ArrayList<V> values = new ArrayList<V>(keys.size());
        Collections.sort(keys);
        for (K key : keys)
            values.add(map.get(key));
        return values;
    }
}

I always like when I can answer in the form of a passing unit test:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

public class SortedMapValuesTest extends TestCase {
    public void testSortedMapValues() throws Exception {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1, 200);
        map.put(2, 300);
        map.put(3, 100);
        List<Integer> list = getSortedMapValues(map);
        assertEquals("[200, 300, 100]", list.toString());
    }

    private <K extends Comparable<K>, V> List<V> getSortedMapValues(Map<K, V> map) {
        ArrayList<K> keys = new ArrayList<K>(map.keySet());
        ArrayList<V> values = new ArrayList<V>(keys.size());
        Collections.sort(keys);
        for (K key : keys)
            values.add(map.get(key));
        return values;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文