Java 如何获取 HashTable 值作为 Arraylist?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2915453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:27:51  来源:igfitidea点击:

how to get HashTable values as Arraylist?

javaarraylisthashtable

提问by Radi

Hashtable hw

哈希表硬件

How can I convert its values to:

如何将其值转换为:

ArrayList <Word> arr

thanks.

谢谢。

采纳答案by jjnguy

Use the ArrayListconstructor that takes a collection.

使用ArrayList接受集合的构造函数。

ArrayList<Word> arr = new ArrayList<Word>(hw.values());

Then every value that was in the HashTablewill be in the new ArrayList.

然后,在 中的每个值HashTable都将在新的ArrayList.

You can find documentation about the constructor in the javadocs.

您可以在javadocs 中找到有关构造函数的文档。

回答by karoberts

ArrayList<Word> arr = new ArrayList<Word>( hw.values() );

回答by Rakesh Juyal

use

hw.values();

it will simply return the Collection (like a List) of Wordobjects.

它将简单地返回对象的集合(如列表)Word



from javadocs

来自 javadocs

values

价值观

public Collection values()

公共集合值()

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

返回此映射中包含的值的集合视图。集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。如果在对集合进行迭代时修改了映射(通过迭代器自己的删除操作除外),则迭代的结果是不确定的。该集合支持元素移除,通过 Iterator.remove、Collection.remove、removeAll、retainAll 和 clear 操作从地图中移除相应的映射。它不支持 add 或 addAll 操作。

回答by Simon K. Gerges

Also you can use

你也可以使用

ArrayList<Word> arr = Collections.list(hw.keys());

for keys as ArrayList

对于键作为 ArrayList