从TreeMap中检索HashMap

时间:2012-10-24 21:16:56

标签: java hashmap treemap

我使用以下复杂的数据结构。

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

然后我使用循环(我也尝试过其他循环)。

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?

我的问题是如何使用这种复杂的数据结构以及如何从离场检索到达?

2 个答案:

答案 0 :(得分:1)

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

,因为当您在flights.clear();arrivals.put(arr, flights);arrivals.clear();之后departures.put(dep, arrivals);来电话时,会清除原始对象(航班和到达时间)。请带上初始化语句,即

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

for循环中或替换该语句如下:

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

departures相同。

现在进行检索:

      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
      }

您可以从flights例如

中检索arrivals

如上所述检索到的每个到达者:

      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 :(得分:0)

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());
}

如果您想在到达和航班之间保持一对一的映射,那么此代码可以正常运行。如果您想保持维护航班组的全局结构,那么您将不得不创建另一个全局gflights对象并将每个航班对象放入其中。