将HashMap的所有值放入数组中是否有方法?

huangapple go评论48阅读模式
英文:

Is there a way to put all the values of a HashMap in an array?

问题

Sure, here's the translated code part:

我有一个HashMap,假设它的条目是`{1=1, 3=2, 4=1, 5=1, 6=1}`。我想创建一个仅包含`[1,2,1,1,1]`的数组,是否可能?

为了提供一些上下文,我试图获取可以使用索引进行迭代的内容,任何类似数组或ArrayList的东西都可以(项目的顺序很重要)。

我尝试使用以下代码将值分配给数组:

for (int j=0; j < frequencies.size(); j++){
for (int i : frequencies.values()) {
arr[j] = i;
}
}


`frequencies.values()`(frequencies是我的HashMap的名称)没有特别有用,我尝试过迭代它(没有成功),将其分配给`Collections`也没有用。我还尝试将`myHashMap.values()`分配给ArrayList,显然没有成功。

您有什么建议?

If you have any further questions or need additional assistance, feel free to ask.

英文:

I have a HashMap, let's say, with entries {1=1, 3=2, 4=1, 5=1, 6=1} . I'd like to be able to create an array just containing [1,2,1,1,1]. Is that possible?

To give some context, I'm trying to get something I can iterate over using an index, and anything like an array or ArrayList would work (the order of the items does matter).

I tried assigning the values to an array using

for (int j=0; j&lt; frequencies.size(); j++){
    for (int i : frequencies.values()) {
        arr[j] = i;
    }
}

frequencies.values() (frequencies is the name of my HashMap) wasn't of particular use, and I tried both iterating over that (didn't work) and assigning it to Collections wasn't useful. I tried assigning myHashMap.values() to an ArrayList as well, which obviously didn't work.

What would you recommend?

答案1

得分: 2

frequencies.values() 的类型是通用的 Collection,不一定是 ArrayList。 因此,要将其转换为 ArrayList,您必须进行 复制 -- 幸运的是,ArrayList 有一个接受通用 Collection 并复制它的构造函数。 因此,您只需编写 new ArrayList&lt;&gt;(frequencies.values())

英文:

The type of frequencies.values() is a generic Collection, not necessarily an ArrayList. As a result, to convert it to an ArrayList, you must copy it -- fortunately, ArrayList has a constructor that accepts a generic Collection and copies it. As a result, all you have to do is write new ArrayList&lt;&gt;(frequencies.values()).

答案2

得分: 1

这是一种有点巧妙但对我来说效果很好的方法... 它假设你的哈希映射中的数值是 Long 型。

从你的 "frequencies" 哈希映射中收集值
1. Collection<Long> values_from_hashmap = frequencies.values();

从值的集合创建一个数组列表
2. ArrayList<Long> listOfValues = new ArrayList<Long>(values_from_hashmap);

嘭,你的 listOfValues 数组列表将包含原始哈希映射中的值。

英文:

This is a kinda hacky way but it works well for me.... it assumes the values of the numbers from your hashmap are Longs.

Collect the values from your &quot;frequencies&quot; hashmap: 
1. Collection&lt;Long&gt; values_from_hashmap = frequencies.values(); 
		
Create an array list from the collection of values:

2. ArrayList&lt;Long&gt; listOfValues = new ArrayList&lt;Long&gt;(values_from_hashmap);

Boom your listOfValues array list will contain the values from the orig hashmap

答案3

得分: 1

以下是翻译好的部分:

"Is there a way to put all the values of a HashMap in an array?" 可以将HashMap的所有值放入数组吗?

"Since you did specify an array as the target you can do it like this." 既然您已经将数组指定为目标,您可以这样做。

"Prints" 打印

"[1, 2, 1, 1, 1]"

英文:

>Is there a way to put all the values of a HashMap in an array?

Since you did specify an array as the target you can do it like this.

Map&lt;Integer,Integer&gt; map = new HashMap&lt;&gt;(Map.of(1,1,3,2,4,1,5,1,6,1));
int[] vals = map.values().stream().mapToInt(Integer::valueOf).toArray();

System.out.println(Arrays.toString(vals));

Prints

[1, 2, 1, 1, 1]

</details>



huangapple
  • 本文由 发表于 2020年7月30日 07:34:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63164006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定