简体   繁体   中英

Java : Print hashmap of lists in O(n)

Is it possible to print hashmap of lists in O(n)?

I was about to do somethin like this:

for(Map.Entry<String, ArrayList<Edge>> e : map.entrySet()){
   for(Edge e1 : e.getValue())
      System.out.println(e.getKey() + " = "+ e1.Out());
}

but this is O(n^2)

my hashmap:

private Map<K, List<Edge<K,E>>> G = null;

I tried to do something like this:

public void getEdges(){
    getEdges(0);
}
private void getEdges(int i){
        if(i==this.G.size()){
            return;
        }else{
        List<Edge<K, E>> edgeList = this.G.get(this.G.keySet().toArray()[i]);
        for(Edge<K, E>  e : edgeList) {
            System.out.println("Edges : "+e.toString());
        }
        getEdges(i+1);
    }
}

But this is not O(n)

but this is O(n^2)

It is actually O(E) where E is the total number of edges.

And you cannot improve on that 1 .


1 - At least, from the complexity perspective.

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