Java – get HashSet from the key of HashMap?

I have a very large (100'000s entry) HashMap Now, I need a HashSet that contains all the keys in this HashMap Unfortunately, HashMap has only one keyset () method, which returns a set instead of a HashSet

What is an effective way to generate such a HashSet using Java?

Solution

Why do you need a HashSet in particular?

Any set has the same interface, so it can usually be used interchangeably, because good practice requires you to use the set interface for all these

If you really need one, you can create one from another For general code, it can be:

Map<B,V> map = ...;
    HashSet<B> set = new HashSet<B>(map.keySet());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>