Pergunta

I started with a HashMap<String, int>

In the first line of this sample code I created an ArrayList<String> with the keySet of the Hashmap. Then I attempted to create an ArrayList<Integer> with the entrySet of the same HashMap. Both of these assignments use the same syntax and process but the integer hashmap is giving the error message. It sounds like the problem comes from the fact that I am trying to use a Set containing int's to populate an Integer ArrayList. I know int is a primitive and Integer is an object but I'm not sure what I need to change in the code.

    ArrayList<String> keys = new ArrayList<String>(ngram.keySet());
    ArrayList<Integer> values = new ArrayList<Integer>(ngram.entrySet());
Foi útil?

Solução

You are calling the entrySet() method, which will give you a Set<Entry<String, Integer>>, not a Set<Integer>.

If you want the values, call the values() method, which will return a Collection<Integer>.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top