File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Would a HashSet be a way to solve this problem?

 
David McWilliams
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is self-explanatory...Still have a doubt let me know

 
David McWilliams
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>:
 
David McWilliams
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic