Java 如何遍历集合<;设置<;i连接>&燃气轮机;

Java 如何遍历集合<;设置<;i连接>&燃气轮机;,java,collections,red5,Java,Collections,Red5,我有这样的想法: something need here = scope.getConnections(); //getConnections() returns Collection<Set<IConnection>> something need here=scope.getConnections(); //getConnections()返回集合 我需要遍历所有连接(即getConnections()返回的内容) 如何做到这一点?Collection set

我有这样的想法:

something need here  = scope.getConnections();

//getConnections() returns Collection<Set<IConnection>>
something need here=scope.getConnections();
//getConnections()返回集合
我需要遍历所有连接(即
getConnections()
返回的内容)

如何做到这一点?

Collection set=scope.getConnections();
Collection<Set<IConnection>> sets = scope.getConnections();

for (Set<IConnection> set : sets) {
  for (IConnection connection : set) {
     //do something
  }
}
用于(集合:集合){ 用于(i连接:设置){ //做点什么 } }
Collection set=scope.getConnections();
用于(集合:集合){
用于(i连接:设置){
//做点什么
}
}
for(Set:scope.getConnections()){
用于(IConnection IConnection:set){
//使用每个i连接
}
}
for(Set:scope.getConnections()){
用于(IConnection IConnection:set){
//使用每个i连接
}
}

我建议您不要以这种方式返回连接。
您的getConnections只能返回

Collection<IConnection>

public Collection<IConnection> getConnections()
{
    return connections;
}

我建议您不要以这种方式返回连接。
您的getConnections只能返回

Collection<IConnection>

public Collection<IConnection> getConnections()
{
    return connections;
}

两个嵌套的for循环答案可能就是您所需要的,但是请注意,您还可以将“connections”集合传递给google collections中的Iterables.concat(),并得到一个“展平”iterable


两个嵌套的for循环答案可能就是您所需要的,但请注意,您也可以将“connections”集合传递给google collections中的Iterables.concat(),并得到一个“展平”iterable


您是否有和IDE?它们中的大多数都有助于编写返回类型值,并且还可以插入集合循环代码。:)是的,我有eclipse,我只是对java非常陌生,eclipseAre你重新发明了连接池吗?我建议使用经过全面开发、测试和维护的第三方连接池框架,如C3P0或Proxool。不,我只是想使用red5媒体服务器您有没有和IDE?它们中的大多数都有助于编写返回类型值,并且还可以插入集合循环代码。:)是的,我有eclipse,我只是对java非常陌生,eclipseAre你重新发明了连接池吗?我建议使用第三方全面开发、测试和维护的连接池框架,如C3P0或Proxool。不,我只是尝试使用red5媒体服务器,您的“泛型”在哪里?泛型在那里,但Bozho没有正确使用代码样式。我帮他修好了。啊,是的,对不起。我很匆忙,没有检查:)你的“泛型”在哪里?泛型在那里,但Bozho没有正确使用代码样式。我帮他修好了。啊,是的,对不起。我很匆忙,没有检查:)scope.getConnections()来自引用的库,所以我不能这么做,我只是在使用itOk。我明白了。如果您只需要所有的连接,我建议您在连接上编写一个包装类或Util方法两个循环。scope.getConnections()来自一个引用库,所以我不能这样做,我只是使用itOk。我明白了。如果您只需要所有连接,我建议您在连接上编写一个包装类或Util方法two循环。
private Set<IConnection> connections;
for (IConnection connection : provider.getConnections()) 
{
    connection.doAction();
}