File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I must admit the topic is new for me. I would really appreciate help in directing me to a resource to refer to.
I have a class called customer, which uses another class called orders. The customer class has a method that calls the constructor of the order class to instantiated order objects. The order object has several attributes including order number. What I want to do is the following:
1. Use a HashMap in the customer class to keep a reference of the order objects using the order ID. How can I do this?
2. I would like to use the HashMap to refer to a specific object�s methods. How can I do this?
Thank you,
Mulugeta

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do you mean something like this?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
This is what I wanted to do. Please correct me if my thinking is illogical.
// This is the main class that calls the constructor of the order class
// to initialize order objects
Customer class:
{
public class Customer
private HashMap customerOrder
public Customer()
{
HashMap customerOrder = new HashMap();
}
// This method calls the order class consturctor � see below.
Public void createOrders(String orderNumber, String orderType, String salesPersonName)
{
// How can I check whether there is an object already create
// which has the orderNumber supplied in this parameter?
// If the object by the orderNumber suplied exists, I do not
// want to create the object.
customerOrder.put(orderNumber, Orders(orderNumber, orderType, salesPersonName);

}
public void orderList()
{
// I would like to print a list that includes all the order
// with the following info order number, order type, sales person name.
// How can I iterate the HashMap and print the info?
}
Customer Order class:
// This is the order class. The customer class initializes all its objects
public class Orders
{
private String orderNumber;
private String orderType;
private String orderLocation;

public Orders(String orderNumber, String orderType, String salesPersonName)
{
this.orderNumber = orderNumber;
this.orderType = orderType;
this.salesPerson
}
[ February 27, 2003: Message edited by: Mulugeta Maru ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

// How can I check whether there is an object already create
// which has the orderNumber supplied in this parameter?

I see a method on HashMap called "containsKey" which returns a boolean. Try that.


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

Call "keySet()" on the HashMap and this gives you a set which contains the keys of your HashMap. You can than use java.util.Iterator on that set to access all of your stored data.
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
William:
I must say thank you for your kind help. I am not sure whether to use the contains() method or the keyset().
And also.
Can I use the following method in Customer class to call a method of a specific Order Object contained in the HashMap.
public void getSalesPersonName(String orderNumber)
{
if(customerOrder.contains(orderNumber)) {
Order.getSalesAmount()
}
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

I am not sure whether to use the contains() method or the keyset()

I don't see a "contains" method on the HashMap class. Do you have the api?
The "keySet" method returns a list of all the keys which are in the current HashMap. You can use that list to help you access the data within the HashMap.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

Can I use the following method in Customer class to call a method of a specific Order Object contained in the HashMap.

Yes, except for the fact that you haven't extracted the Order from the HashMap yet. You need to do a "get()".
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
William:
What you are doing is excellent. You are giving me good directions to research before you say yes or not and it is helpful.
I found the following method in the API :
public boolean containsKey(Object key)
Is this how I will use the get method.
public void getSalesPersonName(String orderNumber)
{
customerOrder.get(orderNumber)

if(customerOrder.contains(orderNumber)) {
Order.getSalesAmount()
}

}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985


HashMap doesn't have a "contains" method, it has a "containsKey" and "containsValue".

The purpose of the "get" method is to return the data. You need to capture that, else there isn't much reason to make the call in the first place.
You need to 1] check that the key being passed in is in the HashMap, 2] if so get the Order object, 3] getSalesAmount().
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
William:
You are right the HashMap does not have a contains method it is containsKey().
So will these two methods be right:
These two methods are used in the Customer class.
/** A method that checks a key in a HashMap and calls
* an object of Order class.
* The method first checks whether the orderNumer exists in the HashMap key set.
/ *
public void getSalesPersonName(String orderNumber)
{
String custOrder = orderNumber
customerOrder.get(custOrder)
if(customerOrder.containsKey(custOrder)) {
Order.getSalesAmount()
}
}

/** This method checks whether orderNumber in the HashMap key set
* and if it is not in the key set it will create an Order Object.
*/
public void createOrders(String orderNumber, String orderType, String salesPersonName)
{
String custOrder = orderNumber
customerOrder.get(custOrder)
if(customerOrder.containsKey(custOrder)) {
// do nothing
}
else {
// create the order object and put the orderNumber as key and the object as a value
// in the customerOrder HashMap
customerOrder.put(orderNumber, Orders(orderNumber, orderType, salesPersonName);
}
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

Please read carefully what I am trying to say. You keep making the same mistakes.

What does the "get" do? It returns data. But unless you save that data it isn't going to be of much use.
You need to: 1] check that the key exists (which you are doing), 2] call the "get" (which you are not doing correct, and in the wrong place), 3] call getSalesAmount().
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
William:
I am sorry. I am not still sure how to use the get method and call an Order Object method.

public void getSalesPersonName(String orderNumber)
{
if(customerOrder.containsKey(orderNumber)) {
customerOrder.get(orderNumber).Order.getSalesAmount()
}
}
public void createOrders(String orderNumber, String orderType, String salesPersonName)
{
if(customerOrder.containsKey(orderNumber)) {
// do nothing
}
else {
customerOrder.put(orderNumber, Orders(orderNumber, orderType, salesPersonName);
}
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

I am not still sure how to use the get method

Looking at the api we see that the get method returns an Object.
What is an "Object"? It is the mother of all classes.
So if you were so say something like:

Than "orderObj" would be an object which is the data / instance of the class which is held in the HashTable at the location we found with the key "orderNumber".
But "orderObj" is an object, it isn't an "Order". To make it an order you need to cast it. Do you have any idea how to do that?
Are you trying to compile the code you are posting here? (Just wondering.)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

I would also suggest taking a look at this book which is the web Thinking In Javaand this chapter has info on HashMaps
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I am getting error message when I try to compile the code. However, I spent time to look how to use cast and modified the code:
public void getSalesPersonName(String orderNumber)
{
if(customerOrder.containsKey(orderNumber)) {
Order orderObject = (Order) customerOrder.getSalesAmount();
}
else {
System.out.println(orderNumber + � This order number does not exist, please try again�);
}
}
I am not sure whether this is the source of the error.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

Please learn how to use the *code* tags, that will make your posts easyer to read. UBB codes
You didn't post the error message so I can't help you with that.
The "cast" looks good. Now you have an "Order" object. So now you should be able to call whatever method you want from that class.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hang on in there, Willims!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think Willims is wrong.
Umm...who is Willims anyway?


Java API Documentation
The Java Tutorial
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I must say thank you to William. You have given me excellent advice and the code works. By the way what good book do you recommend for a beginner.
Very many thanks to William again.
MM
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

By the way what good book do you recommend for a beginner.

Please see previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap