System.out.println(arrivals.size());
BECAUSE When you call flights.clear(); after arrivals.put(arr, flights); or arrivals.clear(); after departures.put(dep, arrivals); This clears the original objects (flights and arrivals). Bring your initialization instructions, i.e.
Map<String, Set<MyObject>> arrivals=new HashMap<String, Set<MyObject>>(); Set<MyObject>(); flights=new HashSet<MyObject>();
in for loops or replace this statement as follows:
if(!flights.isEmpty()){ Set<MyObject> newflights=new HashSet<MyObject>(); newflights.addAll(flights);
The same thing can be done with departures .
Now for the search:
Set<String> arrivalKeys = departures.keySet(); Interator<String> arrIter = arrivalKeys.iterator(); while(arrIter.hasNext()){ String arrKey = arrIter.next(); Map<String, Set<MyObject>> arrivals = departures.get(arrKey );
The same can be done to extract flights from arrivals for example.
for each arrival received as indicated above:
Set<String> flightKeys = arrivals.keySet(); Interator<String> flIter = flightKeys.iterator(); while(flIter.hasNext()){ String flKey = flIter.next(); Set<MyObject> flights = arrivals.get(flKey );
source share