This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Would a HashSet be a way to solve this problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Would a HashSet be a way to solve this problem?" Watch "Would a HashSet be a way to solve this problem?" New topic
Author

Would a HashSet be a way to solve this problem?

David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Hi all,

I have an implementation question for you.

I have a list of objects called Customer. The object has 2 fields:

1. email_address (of customer)
2. product_id (a product they purchased)


The list contains the following sample data. This is just a sample. The list can have any number of different customer objects.

1. joe@web.com 101
2. joe@web.com 105
3. david@web.com 106
4. david@web.com 105
5. david@web.com 110
6. joe@web.com 120

I need to send an email to each distinct customer detailing his customer and product details. In the above example, 2 mails, 1 to joe@web.com and 1 to david@web.com would be sent.


It is easy to send 1 email to every email address in the list but I don't want the customer getting multiple emails. I want to batch send the mail. So joe@web.com would get the following mail:

Here are you orders:
product_id
101
105
120

Have any of you any idea how to do this? For a start, I think I need to generate a distinct email list of strings so that I know how many emails so send. Then use this to search a second list.

Thanks


vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Code is self-explanatory...Still have a doubt let me know

David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73

In the above code, am I correct in saying that even though a brand new ArrayList al is being created here, al.add() will add the product to the array list in hmCustomerList? It does this implicitly?

Also, for my real world example, my customer object has more fields than email and product. Would I be able to replace the ArrayList in hmCustomerList with a list of customer objects? e.g.


Thanks for the code by the way.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You should change that signature, yes, but not the ArrayList part. It's the part inside the ArrayList you should change.

Applying coding against interfaces as well, you get the following signature:
Instead of just the number, you create a Customer object and add that to the list.

That is, unless the customer data is the same in each row. Then you can use a Map<String,Customer> but the Customer object must have a collection of IDs.

On a side note, in the above code one line is repeated. You can move that out of the if-else:
Or, if you use a Map<String,Customer>:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Thanks for the suggestions. I'm currently implementing it.

One question if have, my I have place the code into a method that returns a HashMap but because I am programming to the Map interface,

My compiler is complaining that it cannot convert my return map (of type Map) into a HashMap (as in my method signature return value). If I change the above to

it works.

Any ideas?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

The question would be: Why do you need to cast it to a HashMap? Just keep using it as the Map type, like you were in the method. Unless there was a specific reason to return a HashMap, I would suggest keep using the interface type.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

That's what's called programming against interfaces, as I already mentioned. You use Map instead of HashMap, List instead of ArrayList, etc. That allows you to switch (to a TreeMap or LinkedList for instance) by changing only one single line - the initialization. Well, maybe a few more lines if you also use reassignments.
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Here is my final + working solution. Basically, I have generated a map of customer email addresses and their corresponding list of customer objects. Thanks a million for everyone's help. If anyone else has any questions on this, let me know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

A few hints:

1) declare as Map, not HashMap:

2) iterator is generic so you don't need to cast the return value from its next method. Also, you create a new bean to discard it immediately:
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Thanks. Suggestions implemented but when I try to retrieve the returned map from the generateMap function,



I get this:

incompatible types
found : java.util.Map<java.lang.String,java.util.List<com.CustomerBean>>
required: java.util.HashMap<java.lang.String,java.util.List<com.CustomerBean>>


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Then declare that one as Map as well. And if that causes problems, declare the next occurrence as Map too. Because in the end, you don't need any methods of HashMap, do you?

If you want to know why, consider this: what if you need a TreeMap, because you need the keys sorted? What will you do? Search and replace all occurrences? By declaring as Map instead of as HashMap all you need to change is the few lines where you create the objects.
David McWilliams
Ranch Hand

Joined: Mar 14, 2009
Posts: 73
Rob Prime wrote:Then declare that one as Map as well. And if that causes problems, declare the next occurrence as Map too. Because in the end, you don't need any methods of HashMap, do you?

If you want to know why, consider this: what if you need a TreeMap, because you need the keys sorted? What will you do? Search and replace all occurrences? By declaring as Map instead of as HashMap all you need to change is the few lines where you create the objects.


Suggestion understood, implemented and appreciated.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Would a HashSet be a way to solve this problem?
 
Similar Threads
Sending emails with HTML or not?
How to sort different type of Objects in the list
Returning a result set in an html form
How we will Develop to Send Email in J2ee Application without SMTP server
An Automated Newsletter System