Extract HashMap from TreeMap

I use the 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 get an arrival from shipments?

+4
source share
2 answers
  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 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); //copy elements to new set arrivals.put(arr, newflights); flights.clear(); } 

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

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 ); //use your flights set object } 
+1
source
 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()); } 

If you want to keep a one-to-one mapping between arrival and flight, then this code works. If you want to maintain a global flight set maintenance structure, you will have to create another global gflights object and place each flight object in it.

0
source

Source: https://habr.com/ru/post/1441919/


All Articles