简体   繁体   English

如何将HashTable值作为Arraylist?

[英]how to get HashTable values as Arraylist?

Hashtable hw Hashtable hw

How can I convert its values to: 如何将其值转换为:

ArrayList <Word> arr

thanks. 谢谢。

Use the ArrayList constructor that takes a collection. 使用带有集合的ArrayList构造函数。

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

Then every value that was in the HashTable will be in the new ArrayList . 然后, HashTable中的每个值都将位于新的ArrayList

You can find documentation about the constructor in the javadocs . 您可以在javadocs中找到有关构造函数的文档。

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

Also you can use 你也可以使用

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

for keys as ArrayList 作为ArrayList的键

use 使用

hw.values();

it will simply return the Collection (like a List) of Word objects. 它只会返回Word对象的Collection(如List)。


from javadocs 来自javadocs

values

public Collection values() public Collection values()

Returns a Collection view of the values contained in this map. 返回此映射中包含的值的Collection视图。 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. 如果在对集合进行迭代时修改了映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。 The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. 该集合支持元素删除,它通过Iterator.remove,Collection.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。 It does not support the add or addAll operations. 它不支持add或addAll操作。

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

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