I have something like this:
something need here = scope.getConnections();
//getConnections() returns Collection<Set<IConnection>>
I need to iterate through all connections (the stuff that the getConnections()
returns)
How to do that ?
I have something like this:
something need here = scope.getConnections();
//getConnections() returns Collection<Set<IConnection>>
I need to iterate through all connections (the stuff that the getConnections()
returns)
How to do that ?
Collection<Set<IConnection>> sets = scope.getConnections();
for (Set<IConnection> set : sets) {
for (IConnection connection : set) {
//do something
}
}
for (Set<IConnection> set : scope.getConnections()) {
for (IConnection iConnection : set) {
// use each iConnection
}
}
I would recommend to you not to return connections in the way you do.
Your getConnections has to return only
Collection<IConnection>
public Collection<IConnection> getConnections()
{
return connections;
}
Inside your class you can select the way you want or need to store them
private Set<IConnection> connections;
Consider double loop as a problem in your class design.
If I as a user of your class has to write double-loop every time I will stop using your class. So will do your colleagues.
for (IConnection connection : provider.getConnections())
{
connection.doAction();
}
The two-nested-for-loops answer is probably all you need, but note that you could also pass the 'connections' collection to Iterables.concat() in google-collections, and get out a single "flattened" iterable.