简体   繁体   中英

Retrieving HashMap from TreeMap

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?

     System.out.println(arrivals.size()); //result 0, arrivals is empty. WHY?

BECAUSE When you call flights.clear(); after arrivals.put(arr, flights); or arrivals.clear(); after departures.put(dep, arrivals); , this clears your original objects(flights and arrivals). Please bring your initialization statements ie

        Map<String, Set<MyObject>> arrivals=new HashMap<String, Set<MyObject>>();
        Set<MyObject>(); flights=new HashSet<MyObject>(); 

within the for loops or replace that statement as below:

                if(!flights.isEmpty()){
                   Set<MyObject> newflights=new HashSet<MyObject>(); 
                    newflights.addAll(flights); //copy elements to new set
                   arrivals.put(arr, newflights);
                    flights.clear();
                }

Same you may do with departures .

Now for retrievals:

      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 );
        //use your arrivals map object
      }

Same you can do to retrieve flights from arrivals eg

for each arrivals retrieved as above:

      Set<String> flightKeys = arrivals.keySet();
      Interator<String> flIter = flightKeys.iterator();
      while(flIter.hasNext()){
        String flKey = flIter.next();
        Set<MyObject> flights = arrivals.get(flKey );
        //use your flights set object
      }
arrivals=new HashMap<String, Set<MyObject>>();
departures = new TreeMap<String,  Map<String,  Set<MyObject>>>();
for(String dep: nizDep){
    for(String arr: nizArr){
      for(MyObject flight: _flights){
        if(flight.getFrom().equalsIgnoreCase(dep)&&flight.getTo().equalsIgnoreCase(arr)){
            flights=new HashSet<MyObject>(); 
            flights.add(flight);        
            arrivals.put(arr, flights);     
            departures.put(dep, arrivals);
         }
      }
   }
}
System.out.println(departures.size()); //result 14
if(departures.containsKey("Madrid")) {
    arrivals=departures.get("Madrid");
    System.out.println(arrivals.size());
}

In case you want to keep a one-to-one mapping between arrivals and flights, then this code works. In case you want to keep a global structure of maintaining the set of flights then you'll have to create another global gflights object and put every flights object into it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM