在Java中实例化集合集? [英] Instantiating Set of Sets in Java?

查看:114
本文介绍了在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<>();

这篇关于在Java中实例化集合集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆