简体   繁体   English

尝试创建一个ArrayList <Integer> 带有一组整数

[英]Trying to create an ArrayList<Integer> with a Set of ints

I started with a HashMap<String, int> 我从HashMap<String, int>

In the first line of this sample code I created an ArrayList<String> with the keySet of the Hashmap. 在此示例代码的第一行中,我使用Hashmap的keySet创建了一个ArrayList<String> Then I attempted to create an ArrayList<Integer> with the entrySet of the same HashMap. 然后,我尝试使用相同的HashMap的entrySet创建一个ArrayList<Integer> 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. 听起来问题出在我试图使用包含int的Set填充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. 我知道int是一个原始类型,而Integer是一个对象,但是我不确定我需要在代码中进行哪些更改。

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

You are calling the entrySet() method , which will give you a Set<Entry<String, Integer>> , not a Set<Integer> . 您正在调用entrySet()方法 ,该方法将为您提供Set<Entry<String, Integer>> ,而不是Set<Integer>

If you want the values, call the values() method , which will return a Collection<Integer> . 如果需要这些值,请调用values()方法 ,该方法将返回Collection<Integer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM