Question

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 ?

Was it helpful?

Solution

for (Set<IConnection> set : scope.getConnections()) {
   for (IConnection iConnection : set) {
      // use each iConnection
   }
}

OTHER TIPS

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

for (Set<IConnection> set : sets) {
  for (IConnection connection : set) {
     //do something
  }
}

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.

http://google-collections.googlecode.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top