This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to prevent duplicates?

 
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cruz Loyde wrote:. . . Prof gave sets of instructions on how to do the methods so i really cant do my own thing.

I never like to see assignments specified in such detail. I think that is over‑specification and it prevents you using what may be better solutions like Carey's boolean return type or my suggestion of returning an unmodifiable List.
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . the singular form of media is medium . . .

If you will live somewhere like Holland where they speak better English than in England
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:A personal preference:
I can see throwing an exception if null is passed in, however, I'd rather have a boolean returned as to whether the add was successful or not.



but I forgot the logic pro/cons to use exception. For the above case using return is better rather tan exception propagation.
 
Sheriff
Posts: 6381
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was taught is that you use an exception only to catch processing that is exceptional.  That is, when you are not expecting the condition to happen, but you're protecting against it.  Another way to think of it is you throw an exception when there is no good way to recover from the condition.

You don't want to use an exception to signal the success or failure of a process, because these are normal conditions in the method.  You might want to throw an exception when null is passed because this is often a sign that something has gone wrong with the calling code, but I could also see returning failure (false) in this case too.
 
Rancher
Posts: 4329
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might have missed this in the thread, but if the List<Media> is not supposed to have duplicates, then isn't that a Set?
You can then skip the whole checking thing in the addMedia method, so long as equals has been correctly defined.
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . isn't that a Set? . . .

Of course it is, but I htink OP has been told not to use Sets (not certain). More precisely it would be a set orderd by insertion order, like a linked hash set.
 
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging from OP's replies, he's still a ways off from being able to implement equals() that would work for this problem. This exercise might lead up to that though. He just needs to figure out what "duplicate" means. So far, he hasn't been able to articulate that concept very well so naturally, he's having difficulty coding it.

Edit: I take that back. Missed his last post with the code that actually does what he needed to do.
 
Police line, do not cross. Well, this tiny ad can go through:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!