在Java中实例化集合集?

在Java中实例化集合集?

问题描述:

我想实例化一组(字符串),然后将两个 Set< String> 放入其中,如下所示:

I want to instantiate a Set of Sets (of strings), and then place two Set<String>'s into it, like below:

Set<String> setOne = retrieveSetOne();
Set<String> setTwo = retrieveSetTwo();
Set<Set<String>> myCollection = new HashSet<new HashSet<String<()>(); // not working
myCollection.add(setOne);
myCollection.add(setTwo);

问题是,我对嵌套集的实例化不起作用。我该怎么做?

Problem is, my instantiation of the nested set does not work. How do I do this?

将其更改为

Set<Set<String>> myCollection = new HashSet<Set<String>>();

在创建实例时通过实现初始化,对于需要匹配声明的类型,

you initialize by implementation when you create instance, for type you need to match declaration,

如果您已经使用Java7,那么您只需使用

If you are on Java7 already then you can simply use

Set<Set<String>> myCollection = new HashSet<>();