Java 在android中,如何在不改变值的情况下按字母顺序排序哈希映射键?

Java 在android中,如何在不改变值的情况下按字母顺序排序哈希映射键?,java,android,hashmap,Java,Android,Hashmap,我有一个哈希映射列表,其中联系人姓名作为键,电话号码作为值,如下所示 cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { name = cursor.getString(cursor.getColumnIndex(ContactsC

我有一个哈希映射列表,其中联系人姓名作为键,电话号码作为值,如下所示

   cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {

        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        //stored_Contacts.add(name);
       // phone_number_list.add(phonenumber);


        try {
            hash_map_list = new ArrayList<HashMap<String, String>>();
            map = new HashMap<String, String>();
            map.put(name, phonenumber);
            hash_map_list.add(map);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    cursor.close();
cursor=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null);
while(cursor.moveToNext()){
name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
phonenumber=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
//存储的联系人。添加(姓名);
//电话号码列表。添加(电话号码);
试一试{
hash_map_list=new ArrayList();
map=新的HashMap();
map.put(姓名、电话号码);
哈希映射列表。添加(映射);
}捕获(例外e){
e、 printStackTrace();
}
}
cursor.close();
我需要的是,我想按字母顺序对联系人姓名进行排序,并保留带有联系人姓名的值(电话号码)。有人能帮忙吗

我尝试过这个功能,但不起作用

 private static Map<String, String> sortByComparator(Map<String, String> unsortMap, final boolean order)
{

    List<Map.Entry<String, String>> list = new LinkedList<Map.Entry<String, String>>(unsortMap.entrySet());

    // Sorting the list based on values
    Collections.sort(list, new Comparator<Map.Entry<String, String>>()
    {
        public int compare(Map.Entry<String, String> o1,
                           Map.Entry<String, String> o2)
        {
            if (order)
            {
                return o1.getValue().compareTo(o2.getValue());
            }
            else
            {
                return o2.getValue().compareTo(o1.getValue());

            }
        }
    });

    Map<String, String> sortedMap = new LinkedHashMap<String, String>();
    for (Map.Entry<String, String> entry : list)
    {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}
private static Map sortByComparator(Map-unsortMap,最终布尔顺序)
{
List=新的LinkedList(unsortMap.entrySet());
//根据值对列表进行排序
Collections.sort(list,newcomparator()
{
公共整数比较(Map.Entry o1,
地图入口(o2)
{
如果(订单)
{
返回o1.getValue().compareTo(o2.getValue());
}
其他的
{
返回o2.getValue().compareTo(o1.getValue());
}
}
});
Map sortedMap=new LinkedHashMap();
用于(映射条目:列表)
{
sortedMap.put(entry.getKey(),entry.getValue());
}
返回分拣的DMAP;
}

A
HashMap
不是为排序数据集而设计的。为了在存储和检索值时尽可能提高效率,牺牲了这一点


另一方面,
TreeMap
的设计方式使您的键始终处于排序状态。这看起来是适合您的用例的工具。

A
HashMap
不是为排序数据集而设计的。为了在存储和检索值时尽可能提高效率,牺牲了这一点


另一方面,
TreeMap
的设计方式使您的键始终处于排序状态。这看起来是适合您的用例的工具。

使用
TreeMap
而不是
HashMap
。完成。为什么不使用arraylist?@pavan,我如何使用arraylist?选中此项。如果您在列表中显示它,它可以帮助满足您的要求,否则,如果在列表中,您可以使用一个对象创建一个arraylist,该对象包含所有值,如name,如果要添加任何不需要更改代码的额外属性,也可以添加一个新属性。使用
TreeMap
而不是
HashMap
。完成。为什么不使用arraylist?@pavan,我如何使用arraylist?选中此项。如果您在列表中显示它,它可以帮助满足您的要求,否则,如果在列表中,您可以使用一个对象创建一个arraylist,该对象包含所有值,如name,如果你想添加任何额外的属性,你不需要改变你的代码,你可以只添加一个新的属性。