Results 1 to 1 of 1
  1. #1
    whizzz is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default (Help) Hashtable for adjacency list

    hey people,
    i have a little problem about implementing a Hashtable in my project.
    i want to store nodes and the edges between them in the hashtable.

    Java Code:
    Map<City, List<City>> adj= new HashMap<City, List<City>>();
    // add a connection from -> to
    public boolean add(City from, City to) {
       List<City> tos= adj.get(from);
       if (tos == null) // from is not in the map
          return false;
       tos.add(to); // add a new to 
       return true;
    // add a single airport
    public boolean add(City airport) {
       List<City> tos= adj.get(airport);
       if (tos != null) // already in the list
          return false;
       tos= new ArrayList<City>();
       adj.put(airport, tos); // add airport and a new empty adj. list
       return true;
    the example of "Josah" is really close but my problem occurs when i want to know the predecessors or successors of selected nodes(Cities in this case).
    for example i want to implement my edges like this;
    p and s are Cities and im adding and edge from p city to s city

    so for the methods of "addpredecessor" and "addsuccesors" , how can i use the hashtable and should i use 2 different lists for 2 different methods ?
    in that case, how its possible to connect a node (City) to 2 different lists(predecessors, successors) in Hastable? so i can call the selected Cities predecessors or successors any time from one Hashtable.
    Last edited by whizzz; 10-21-2010 at 01:56 PM.

Similar Threads

  1. Implementing an Adjacency List for a Flight Map
    By javaRancher2009 in forum New To Java
    Replies: 3
    Last Post: 12-07-2009, 04:45 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 09:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 09:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts