• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to prevent duplicates?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic