List < String > 到 ArrayList < String > 转换问题

我有一个方法... ... 实际上是把一系列的句子分成几个单词。下面是这个方法:

public List<String> getWords(List<String> strSentences){
allWords = new ArrayList<String>();
Iterator<String> itrTemp = strSentences.iterator();
while(itrTemp.hasNext()){
String strTemp = itrTemp.next();
allWords = Arrays.asList(strTemp.toLowerCase().split("\\s+"));
}
return allWords;
}

我必须以下面的格式将这个列表传递到散列表中

HashMap<String, ArrayList<String>>

这个方法返回 List,我需要一个 ArrayList?如果我试图投它不工作... 有什么建议吗?

另外,如果我将 ArrayList 更改为 HashMap 中的 List,我会得到

java.lang.UnsupportedOperationException

因为我的代码中有这一行

sentenceList.add(((Element)sentenceNodeList.item(sentenceIndex)).getTextContent());

有更好的建议吗?

258451 次浏览

Arrays.asList does not return instance of java.util.ArrayListbut it returns instance of java.util.Arrays.ArrayList.

You will need to convert to ArrayList if you want to access ArrayList specific information

allWords.addAll(Arrays.asList(strTemp.toLowerCase().split("\\s+")));

Cast works where the actual instance of the list is an ArrayList. If it is, say, a Vector (which is another extension of List) it will throw a ClassCastException.

The error when changing the definition of your HashMap is due to the elements later being processed, and that process expects a method that is defined only in ArrayList. The exception tells you that it did not found the method it was looking for.

Create a new ArrayList with the contents of the old one.

new ArrayList<String>(myList);

Take a look at ArrayList#addAll(Collection)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behaviour of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behaviour of this call is undefined if the specified collection is this list, and this list is nonempty.)

So basically you could use

ArrayList<String> listOfStrings = new ArrayList<>(list.size());
listOfStrings.addAll(list);

First of all, why is the map a HashMap<String, ArrayList<String>> and not a HashMap<String, List<String>>? Is there some reason why the value must be a specific implementation of interface List (ArrayList in this case)?

Arrays.asList does not return a java.util.ArrayList, so you can't assign the return value of Arrays.asList to a variable of type ArrayList.

Instead of:

allWords = Arrays.asList(strTemp.toLowerCase().split("\\s+"));

Try this:

allWords.addAll(Arrays.asList(strTemp.toLowerCase().split("\\s+")));

In Kotlin List can be converted into ArrayList through passing it as a constructor parameter.

ArrayList(list)

Tried and tested approach.

public static ArrayList<String> listToArrayList(List<Object> myList) {
ArrayList<String> arl = new ArrayList<String>();
for (Object object : myList) {
arl.add((String) object);
}
return arl;


}

This comes a bit late but thought of putting in a simple way for the answer of converting List to ArrayList.

Simple way

public static <T> ArrayList<T> listToArrayList(List<T> list) {
return list != null ? new ArrayList<>(list) : null;
}

Classic way

public static  <T> ArrayList<T>  listToArrayList(List<T> list) {
ArrayList<T> arrayList = new ArrayList<>();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
arrayList.add(list.get(i));
i++;
}
// or simply
// arrayList.addAll(list);
}
return  arrayList;
}

Usage

listToArrayList(yourList);

ArrayList listOfStrings = new ArrayList<>(list.length); listOfStrings.addAll(Arrays.asList(list));

In case of object list you can use this way to convert Model[] list to ArrayList