The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


You have all your solutions...

Posted by Kishori Sharan on September 28, 2000 at 10:14 AM

Never mind if I didn't get your questions correctly and have suggested some wrong/bad solutions.

Solution 1. Your first problem is that you are getting an object from entry set which holds an int value and you want that int value to compare it with customet arrival time.
int availableTime = entry.getValue();// you are geting error here

Here you can write the following code
Integer aTimeObject = ( Integer ) entry.getValue ( ) ;
int availableTime = aTimeObject.intValue ( ) ;
Now your availableTime gets the correct int value and you can compare it with customerArrivalTime which is int.

Solution 2:
When you are iterating thru your entry set and if you say entry.setValue ( an_object ) then it sets the value of the current entry to an_object . The following java program illustrates the use of setValue ( ) method in Map.Entry interface.

///////////////// //////////////////////////
import java.util.* ;

class Test {

public static void main(String[] args) throws Exception {

// Create a map
HashMap hm = new HashMap ( ) ;

// Make some data in map
hm.put ( "1", "Hello" ) ;
hm.put ( "2", "Hi" ) ;

// Print the values in hash map
System.out.println ( hm ) ;

// Get the entry set. Note that the Set s contains an object of
// type Map.entry .
Set s = hm.entrySet ( ) ;

// Get an iterator for the set
Iterator i = s.iterator ( ) ;

// Loop thru all the set element ( in fact the entryset elements)
while ( i.hasNext ( ) ) {
// Get the map entry element
Map.Entry me = ( Map.Entry) ;

// Set the value to "Thanx". This will change all the two entries to
// the same String "Thanx" bacause we are iterating thru whole entry
// If you want to use setValue selectively then just check the value
// in "me" object and then use setValue ( ). This setting will be confirmed
// by printing the new values in hashmap at the end of this program
me.setValue ( "Thanx" ) ;

// Verify the change
System.out.println ( hm ) ;

/////////////////////// ends here//////////////////

Solution 3:
If you want to hold the arrival times for all customer then you can keep it in an instance variable of the class. That instance variable could be a reference to just an object and the object could be two dimensional array of int/double ( depending on if your time can be in fraction ) , or it could be a HashMap which will hold customer id and his arrival time ( I assume customer comes only once ) . If you have duplicate customer id problem in hashmap then you can simply consider string these pairs in a two dimensional String array.
If you uncertain about the number of customer/his_arrival_time pairs then you can think of using Vector. There are many possible solution to your 3rd problem. You have to decide which solution you want to implement depending on your requirements and available resources.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us