473,804 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems getting an object from hashmap

1 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. class PlaceGraph<N> implements Graph <N> {
  2.  
  3.     private Map<N, List<Edge<N>>> allp = new HashMap <N, List<Edge<N>>>();
  4.  
  5.  
  6.     public void addNode(N node){
  7.     if (!allp.containsKey(node))
  8.         allp.put(node, new ArrayList<Edge<N>>());
  9.     else 
  10.         System.out.println ("Same name found");
  11.     }
  12.  
  13.  
  14.     public void connect (N from, N to, String med, int traveltime){
  15.  
  16.     List<Edge<N>> flist = allp.get(from);
  17.     List<Edge<N>> tlist = allp.get(to);
  18.  
  19.     if (flist == null || tlist ==null)
  20.         throw new NoSuchElementException();
  21.     if (traveltime<0)
  22.         throw new IllegalArgumentException();
  23.  
  24.  
  25.     Edge<N> e1 = new Edge<N> (to, med, traveltime);
  26.     flist.add(e1);
  27.     Edge<N> e2 = new Edge<N> (from, med, traveltime);
  28.     tlist.add(e2);
  29.     }
  30.  
  31.  
  32.     PROBLEM HERE/public void find(N b, N d){
  33.  
  34.     }
  35.  
  36.       public String toString(){
  37.       String str = " ";
  38.     for (Map.Entry<N, List<Edge<N>>> p : allp.entrySet()){
  39.         str+=p.getKey() + " : ";
  40.         for (Edge e : p.getValue())
  41.         str += e.toString()+ " ";
  42.         str += "\n";
  43.     }
  44.     return str ;
  45.       }
  46. }
  47.  
  48. //Edge class contains three variables. Destination, medium of travel and traveltime.
  49.  
Mar 20 '08 #1
0 1159

Sign in to post your reply or Sign up for a free account.

Similar topics

1
16805
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes very ugly and unreadable soon. I'm aware that there exists the for_each-template, but this doesn't make the code any better because the body of the for-loop must then live in an extra function (consider two nested loops). Now I tried to mimic the...
4
5768
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString, HashStringCompare>() {} };
6
2344
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is there a shortcut for creating such array ? eg //simple array indexed via numbers var a = new Array("a", "fdsf", "sada"); or,
2
10772
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... public HashMap<Position, Integer> boardNumbers; .... and ... HashMap<Integer, Integer> allNumbers = new HashMap<Integer, Integer>();
4
1945
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007, ItemPrice= 3.99, ItemNumber=8504, ItemQuantity=1}, ShipMethodID=75, ItemCount=1, Items_StatusID=2, Items_Status=Shipped, ShipMethod=, Tracking_TrackingNumber=, ShippingLevelID=1} entrySet3: Outcome={ReturnCode=0, ReturnMessage}
15
5678
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
1
3909
by: viswanathtvs | last post by:
java.util.NoSuchElementException javax.faces.el.EvaluationException: java.util.NoSuchElementException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:387) at...
3
5408
dlite922
by: dlite922 | last post by:
I could have this done in php in three lines of code but in Java here's what I need to do: English Looping through list of words I call a function that returns a HashSet of movie titles that contain that particular word. I want to insert these titles into a HashMap with their value being a counter of how many times this title occured. (if two words return the same title, that title's value (counter) would be 2) Relative Code:
1
5625
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the key, where the Integer is "1", and String from the value. How could I iterate it? I wrote that, but it doesn't work at the way I expected: HashMap<Dimension, String> singleValues = new HashMap<Dimension, String>();
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.