Java 将链接的 hashmap 转换为字符串数组

Java turn linked hashmap to a string array

我有一个 linkedhash 映射,它的键是句子编号,值是句子的分数。

4=104
3=104
1=106
7=130
8=139
9=168
2=199
5=330

我需要保持这个顺序,因为它是一个文本摘要程序。我还有一个字符串数组,其中包含使用标点符号分隔的每个句子。我将如何创建一个新的字符串数组,第一个键将是与 linkedhash 映射中的第一个键对应的句子 4。数组中的最后一个键是句子 5。

谢谢

类似 -

List<String> orderedSentenceList = new ArrayList<String>();
for (Map.Entry<String, String> entry : yourMap.entrySet()) {
    String key = entry.getKey();
    orderedSentenceList.add(originalSentenceList.get(key-1))
}

现在使用 orderedSentenceList

这是您要找的吗?

import java.util.Map.Entry;

String[] array = new String[map.size()];
int i = 0;
for (Entry<Integer, String> entry : map.entrySet()) {
    array[i++] = entry.getValue();
}

我希望我理解你的问题,但这里是:

这只是根据您的要求使用 String[]

String[] sentenceList = new String[yourLinkedHashMap.size()];
for (Map.Entry<Integer, String> entry : yourLinkedHashMap.entrySet()) {
   sentenceList[entry.getKey() - 1] = entry.getValue();
}

类似这样的内容将帮助您开发自己的解决方案

 String []sentences=new String[mp.size()] ;
 //mp is the map
    Iterator it = mp.entrySet().iterator();
    int i=0;
    while (it.hasNext()) {
         Map.Entry pair = (Map.Entry)it.next();
         sentences[i++]=pair.getValue();
    }