I use following complex data structure.
departures = new TreeMap<String, Map<String, Set<MyObject>>>();
arrivals=new HashMap<String, Set<MyObject>>();
flights=new HashSet<MyObject>();
Then I use loops (I also tried other loops).
for(String dep: nizDep){
for(String arr: nizArr){
for(MyObject flight: _flights){
if(flight.getFrom().equalsIgnoreCase(dep)&&flight.getTo().equalsIgnoreCase(arr)){
flights.add(flight);
}
}
if(!flights.isEmpty()){
arrivals.put(arr, flights);
flights.clear();
}
}
if(!arrivals.isEmpty()){
departures.put(dep, arrivals);
arrivals.clear();
}
}
System.out.println(departures.size()); //result 14
System.out.println(departures.containsKey("Madrid")); //result true
arrivals=departures.get("Madrid");
System.out.println(arrivals.size()); //result 0, arrivals is empty. WHY?
My question is how to use this complex data structure and how to retrieve arrivals from departures?
BECAUSE When you call
flights.clear();afterarrivals.put(arr, flights);orarrivals.clear();afterdepartures.put(dep, arrivals);, this clears your original objects(flights and arrivals). Please bring your initialization statements i.e.within the
forloops or replace that statement as below:Same you may do with
departures.Now for retrievals:
Same you can do to retrieve
flightsfromarrivalse.g.for each arrivals retrieved as above: