Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何根据映射值的第一个字母对其进行排序?_Java_Sorting_Dictionary_Linkedhashmap - Fatal编程技术网

Java-如何根据映射值的第一个字母对其进行排序?

Java-如何根据映射值的第一个字母对其进行排序?,java,sorting,dictionary,linkedhashmap,Java,Sorting,Dictionary,Linkedhashmap,我有一个Java映射,我使用此代码按字母顺序对其字符串值进行排序: public <K, V> LinkedHashMap<K, V> sortMapByValues( Map<K, V> map ) { SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>( new Comparator<Ma

我有一个Java映射,我使用此代码按字母顺序对其字符串值进行排序:

public <K, V> LinkedHashMap<K, V> sortMapByValues( Map<K, V> map ) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(
        new Comparator<Map.Entry<K, V>>() {
            @Override 
            public int compare( Map.Entry<K, V> e1, Map.Entry<K, V> e2 ) {
                // Sort this word alphabetically in the map : 
                String a = (String)e1.getValue();
                String b = (String)e2.getValue();

                int diff = a.compareToIgnoreCase( b );

                if (diff == 0) 
                    diff = a.compareTo(b);  

                return diff != 0 ? diff : 1;  // Fix words having the same spelling.
            }
        }
    );

    sortedEntries.addAll( map.entrySet() );

    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>();

    for( Map.Entry<K, V> sortedEntry: sortedEntries )
        sortedMap.put( sortedEntry.getKey(), sortedEntry.getValue() );

    return sortedMap;
}
public LinkedHashMap sortmapbyvalue(映射映射){
SortedSet sortedEntries=新树集(
新比较器(){
@凌驾
公共整数比较(映射项e1、映射项e2){
//在地图中按字母顺序排列此单词:
字符串a=(字符串)e1.getValue();
字符串b=(字符串)e2.getValue();
int diff=a.比较信号情况(b);
如果(差异==0)
差异=a.与(b)相比;
return diff!=0?diff:1;//修复拼写相同的单词。
}
}
);
sortedEntries.addAll(map.entrySet());
LinkedHashMap sortedMap=新LinkedHashMap();
用于(地图入口分拣机:分拣机)
sortedMap.put(sortedEntry.getKey(),sortedEntry.getValue());
返回分拣的DMAP;
}
因为Map有数千个值,所以上面的代码运行速度足够快,可以快速得到我想要的结果。现在,我需要更改此代码并更新它,以便根据另一个条件对映射值进行排序,而不是按字母顺序进行排序

我有一个不同的字母数组列表,比如:

ArrayList lettersArrayList = new ArrayList<String>( Arrays.asList( "E", "C", "A", "Z", "Q", "R", "B", "L", "D", ... ) );
ArrayList-lettersArrayList=新的ArrayList(Arrays.asList(“E”、“C”、“A”、“Z”、“Q”、“R”、“B”、“L”、“D”、…));

此ArrayList中的字母值由用户指定,因此它们可能具有任何其他字母值和顺序我需要根据这个ArrayList对映射的字符串值进行排序,因此以“E”开头的单词排在第一位,然后是以“C”开头的单词,依此类推。这可能吗?

首先,您的比较器不正确:

return diff != 0 ? diff : 1;
如果
a
b
具有相同的拼写,将
a
b
进行比较得到1,表示
a>b
,将
b
a
进行比较也得到1,表示
b>a
。你可以用

return diff != 0 ? diff : Integer.compare(System.identityHashCode(e1), System.identityHashCode(e2));
(几乎)正确。如果您使用了大量内存,并且两个单独的对象恰好以相同的系统哈希代码结束,那么这仍然可以使两个条目相等,而实际上它们是不同的,这是非常非常不可能的

现在,为了回答您的问题,您只需比较两个条目的首字母索引:

String a = (String)e1.getValue();
String b = (String)e2.getValue();

int index1 = list.indexOf(a.substring(0, 1));
int index2 = list.indexOf(b.substring(0, 1));

int diff = Integer.compare(index1, index2);
这将起作用,但效率极低,因为

  • indexOf()是O(n)
  • 最好使用字符而不是字符串来存储单个字符

因此,您不应该使用
列表
来存储字母,而应该使用
HashMap
,其中每个字母都与其位置相关联。此映射中的查找将是O(1),这将使比较器更快。

非常感谢您提供了详细的答案,并更正了我的代码。至于使用字符而不是字符串,我已经在这么做了,但为了一些测试目的,我刚刚更改了代码。我将使用字符对其进行编码,以提供更快的性能。