Problems getting an object from hashmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trask
    New Member
    • Mar 2008
    • 1

    #1

    Problems getting an object from hashmap

    hi,

    I am troubled by a small problem. I am now using a graph metod which uses edge to connect various places in a map. So i created a hashmap which takes in String
    and an arraylist (edge) which states the destinations from that string along with the travel time.

    But how to find connections between two places?

    t.ex, a to b takes 15mins by buss , b to c takes 10mins by train,
    so if i want to find connection between a to c, it should show me exactly that and the total time.

    To add new places and to connect them works. Only to find connections is the problem..

    Thanks in forehand

    pasting the java codes.

    [code=java]
    class PlaceGraph<N> implements Graph <N> {

    private Map<N, List<Edge<N>>> allp = new HashMap <N, List<Edge<N>>>( );


    public void addNode(N node){
    if (!allp.contains Key(node))
    allp.put(node, new ArrayList<Edge< N>>());
    else
    System.out.prin tln ("Same name found");
    }


    public void connect (N from, N to, String med, int traveltime){

    List<Edge<N>> flist = allp.get(from);
    List<Edge<N>> tlist = allp.get(to);

    if (flist == null || tlist ==null)
    throw new NoSuchElementEx ception();
    if (traveltime<0)
    throw new IllegalArgument Exception();


    Edge<N> e1 = new Edge<N> (to, med, traveltime);
    flist.add(e1);
    Edge<N> e2 = new Edge<N> (from, med, traveltime);
    tlist.add(e2);
    }


    PROBLEM HERE/public void find(N b, N d){

    }

    public String toString(){
    String str = " ";
    for (Map.Entry<N, List<Edge<N>>> p : allp.entrySet() ){
    str+=p.getKey() + " : ";
    for (Edge e : p.getValue())
    str += e.toString()+ " ";
    str += "\n";
    }
    return str ;
    }
    }

    //Edge class contains three variables. Destination, medium of travel and traveltime.
    [/code]
    Last edited by Nepomuk; Sep 5 '08, 10:29 AM. Reason: Added Code tags
Working...