• 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?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Im not sure if my code is correct but i would like some feedback.
 
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i would like some feedback


How does the code prevent duplicates?
It looks like it will always call the add method.
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

i would like some feedback


How does the code prevent duplicates?
It looks like it will always call the add method.





Im not really sure what im doing, im also not allowed to use hashnet since it wasnt taught.
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what that code is supposed to do, and whether there is supposed to be any relationship to the thre‍ad title. I can see nothing in there that would prevent duplication. Please remove the loop, because it doesn't do anything. What you are doing is iterating something, then testing whether its title equals itself, then getting a title and not doing anything with it. So the loop takes time to achieve nothing.
Then you are adding the object to the Iterable, unless it is null.

Don't have a field and a parameter with the same name; that is a good source of confusion. It is maybe better to throw a null pointer exception f you are presented with a null, or better still condense the method toThat will throw the exception for you if you pass null. But I can't say more until I know what you mean about duplicates.
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Please explain what that code is supposed to do, and whether there is supposed to be any relationship to the thre‍ad title. I can see nothing in there that would prevent duplication. Please remove the loop, because it doesn't do anything. What you are doing is iterating something, then testing whether its title equals itself, then getting a title and not doing anything with it. So the loop takes time to achieve nothing.
Then you are adding the object to the Iterable, unless it is null.

Don't have a field and a parameter with the same name; that is a good source of confusion. It is maybe better to throw a null pointer exception f you are presented with a null, or better still condense the method toThat will throw the exception for you if you pass null. But I can't say more until I know what you mean about duplicates.



What i planned is that to use the for loop to go over my list and remove the duplicates. I refered my code to this. Since its trying to find the longest i thought i could use it aswell to remove the duplicate.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

use it aswell to remove the duplicate.


Removing duplicates is different from preventing duplicates.
Which is the goal?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

use it aswell to remove the duplicate.


Removing duplicates is different from preventing duplicates.
Which is the goal?



Prevent, sorry about that.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class is the container that holds the items?  Many classes have methods to detect if they hold an item.
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What class is the container that holds the items?  Many classes have methods to detect if they hold an item.



 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the API doc for the ArrayList class to see if any of its methods would be useful?
http://docs.oracle.com/javase/8/docs/api/index.html
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you look at the API doc for the ArrayList class to see if any of its methods would be useful?
http://docs.oracle.com/javase/8/docs/api/index.html





This is my new code, still struggling why it would not pass my tests.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try any of the ArrayList methods?  They would be better than your search loop.

If the object is to NOT add a duplicate, what is the purpose of the duplicate variable and remove method?

If the list contains an item that is the same as the one to be added, why not just exit the method when the match is found and NOT add the new item?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think to add an object to the list. The 3rd test is failing.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The 3rd test is failing.


What line(s) is the 3rd test?

How can anyone compile and execute the code for testing?  Missing imports and class definitions
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

The 3rd test is failing.


What line(s) is the 3rd test?

How can anyone compile and execute the code for testing?  Missing imports and class definitions



import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import edu.westga.cs1301.media_library.model.Media;
import edu.westga.cs1301.media_library.model.MediaLibrary;

line 36 is failing
 
Norm Radder
Rancher
Posts: 3523
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

line 36 is failing


I see line 36 to be:  @Test
How does that line fail?

Is there an error message?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

line 36 is failing


I see line 36 to be:  @Test
How does that line fail?

Is there an error message?



Starting from 36 to 50, thats for testing if duplicate is prevented from adding. What im getting is that i have 3 objects in my list instead of 2 even tho im trying to prevent the duplicate from adding
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

im trying to prevent the duplicate from adding


Can you post the code for the method that prevents adding a duplicate?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

im trying to prevent the duplicate from adding


Can you post the code for the method that prevents adding a duplicate?



 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see my earlier post?


If the object is to NOT add a duplicate, what is the purpose of the duplicate variable and remove method?

If the list contains an item that is the same as the one to be added, why not just exit the method when the match is found and NOT add the new item?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you see my earlier post?


If the object is to NOT add a duplicate, what is the purpose of the duplicate variable and remove method?

If the list contains an item that is the same as the one to be added, why not just exit the method when the match is found and NOT add the new item?



Welp Idk anymore, i give up, GG

Thanks for the help anyway.
 
Norm Radder
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic would be like this:
Check if item is in list
If item is not in list, add item

No remove, no counting duplicates.
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:The logic would be like this:
Check if item is in list
If item is not in list, add item

No remove, no counting duplicates.



I FIGURED IT OUT, I WAS SO HAPPY LOL!!
 
Norm Radder
Rancher
Posts: 3523
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure with that code?  It only allows 1 of any category.
Should the test compare BOTH the title AND the category to determine a match?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Are you sure with that code?  It only allows 1 of any category.
Should the test compare BOTH the title AND the category to determine a match?



Oh yeah thank you for pointing that out. Is ths better and/or correct one?

 
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Consider implemening/overriding equals() method in your Media class, so you could write:

Campbell already mentioned about the bad idea of having field and parameter names to be the same. It is a matter of time when you make a mistake (I'm assuming you haven't made one already).
 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also by looking to this line you can see, that "this.media" represents a collection of media, while "media" a singular media. That's probably an another reason to follow Campbell's advice.
 
Sheriff
Posts: 6363
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when currMedia and Media have the same title but not the same category?  Is that the behavior you want?
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:What happens when currMedia and Media have the same title but not the same category?  Is that the behavior you want?


I think so because the object sets up like this
Correct me if im wrong.
 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:https://www.ilikeyouroldstuff.com/news/11-songs-that-are-also-movie-titles


 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Good job!
 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd think whether that change would make sense: theMedia => mediaToAdd. Much more expressive. Reveals an intent what you are about to do with it.

Read about equals() method, here. Might be your next improvement.
 
Bartender
Posts: 3606
151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside: the singular form of media is medium (well, in Holland anyway). But admitted: many people talk about 'medias' as plural.

@OP
are you being taught about the 'equals' method? Campbell and Liutauras mentioned it, but it would simplify your code.
 
Cruz Loyde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:As an aside: the singular form of media is medium (well, in Holland anyway). But admitted: many people talk about 'medias' as plural.

@OP
are you being taught about the 'equals' method? Campbell and Liutauras mentioned it, but it would simplify your code.


I was only taught to use .equals like that
 
Saloon Keeper
Posts: 6416
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Marshal
Posts: 7264
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cruz Loyde wrote:I was only taught to use .equals like that


And that's exactly what you are being adviced to do here too. But before that, you need to do some work to make it work. What that work is - well, you got some stuff to research
 
Cruz Loyde
Greenhorn
Posts: 17
  • 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.


Prof gave sets of instructions on how to do the methods so i really cant do my own thing.
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example of how to use the same word to mean different things and to create an opportunity to confuse yourself.Goodbye encapsulation. Goodbye control over your List. Don't return the List unchanged. I can think of ive ways to return a List from such a method:-
  • 1: Simply erturn the List without other ado; that allows other code to pass the List around, change anything in it, and mess up your original object without your knowing anything about it.
  • 2: Copy the List: return new XYZList(list); That provides a “snapshot” of your List at the time of return, which other code can modify to its heart's content without affecting the original. In all cases other code can modify the elements of the List if they are mutable.
  • 3: Return an unmodifiable List, which doesn't mean fixed but read‑only. Any changes in the original are immediately reflected in the copy. return Collections.unmodifiableListlist);
  • 4: Return an unmodifiable copy of a copy: return Collections.unmodifiableList(new XYZList(list)); This is a read‑only copy of a copy which you have discarded all references to, so it is not possible to change the copy.
  • 5: Create an immutable List (Java9+ only) this doesn't allow null elements: return List.of(list.toArray()); The returned List cannot be changed.
  • At least three of those options require you to declare the return type as List<PQR> not ArrayList<PQR> and the types of the variables assigned to from those methods must also be List<PQR>.
     
    Don't MAKE me come back there with this tiny ad:
    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!