List <String> to ArrayList <String> conversion problem

I have the following method ... which actually takes a list of sentences and breaks each sentence into words. There he is:

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; } 

I need to transfer this list to a hash map in the following format

 HashMap<String, ArrayList<String>> 

so this method returns a List and I need an array of List? If I try to drop it, this is not training ... any suggestions?

Also, if I change the ArrayList to List in the HashMap, I get

 java.lang.UnsupportedOperationException 

because of this line in my code

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

Any best deals?

+65
java arraylist list
Oct 30 '12 at 8:12
source share
5 answers

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

Arrays.asList does not return java.util.ArrayList , so you cannot assign the return value of Arrays.asList variable of type ArrayList .

Instead:

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

Try the following:

 allWords.addAll(Arrays.asList(strTemp.toLowerCase().split("\\s+"))); 
+40
Oct 30 '12 at 8:18
source share

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

The error when changing the definition of your HashMap is due to the processing of elements that are being processed, and this process expects a method that is defined only in ArrayList . An exception tells you that he did not find the method he was looking for.

Create a new ArrayList with the contents of the old.

 new ArrayList<String>(myList); 
+141
Oct 30 '12 at 8:16
source share

Take a look at ArrayList # addAll (Collection)

Adds all the elements of the specified collection to the end of this list, in the order in which they are returned by the specified Iterator collection. The behavior of this operation is undefined if the specified collection changes during progress. (This means that the behavior of this call is undefined if the specified collection is a list and this list is not empty.)

So you can use

 ArrayList<String> listOfStrings = new ArrayList<>(list.size()); listOfStrings.addAll(list); 
+27
Oct 30 '12 at 8:17
source share

Arrays.asList does not return an instance of java.util.ArrayList but returns an instance of java.util.Arrays.ArrayList .

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

 allWords.addAll(Arrays.asList(strTemp.toLowerCase().split("\\s+"))); 
+5
Oct 30 '12 at 8:14
source share

In Kotlin, a List can be converted to an ArrayList by passing it as a constructor parameter.

 ArrayList(list) 
0
Jan 12 '19 at 8:44
source share



All Articles