How to prevent duplicates in a basic way? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to prevent duplicates in a basic way?

public void addMedia(Media media) { if(media == null) { throw new IllegalArgumentException("Media can not be null"); } this.media.add(media); } } How can i use the for each to prevent duplicates?

3rd Nov 2019, 6:47 PM
Secr
Secr - avatar
14 Answers
+ 2
Secr Now something like this makes my day. Thanks for letting me know.
3rd Nov 2019, 8:01 PM
Avinesh
Avinesh - avatar
+ 1
@Avinesh Omg it worked thank you very much!
3rd Nov 2019, 8:00 PM
Secr
Secr - avatar
+ 1
If you have time then kindly mail me the whole code so that I can go through it and get back to you tomorrow. But what I suggested should have worked for you.
3rd Nov 2019, 8:47 PM
Avinesh
Avinesh - avatar
0
Probably try making a HashSet because sets cannot hold duplicates and when you print the values the duplicates won't exist in the result.
3rd Nov 2019, 6:57 PM
Avinesh
Avinesh - avatar
0
my Prof. havent taught that yet, so im not allowed to use it.
3rd Nov 2019, 7:01 PM
Secr
Secr - avatar
0
Secr Where are you storing your media objects?
3rd Nov 2019, 7:02 PM
Avinesh
Avinesh - avatar
0
public MediaLibrary() { this.media = new ArrayList<Media>(); }
3rd Nov 2019, 7:08 PM
Secr
Secr - avatar
0
So you are storing everything in an ArrayList. What you can do is probably run a for loop at the end of storing every object and compare every object with every other object and if there is a match then do- ReferenceVariable.remove(index)
3rd Nov 2019, 7:13 PM
Avinesh
Avinesh - avatar
0
Secr if you don't get it then let me see what your final ArrayList looks like so that I can explain you that loop.
3rd Nov 2019, 7:15 PM
Avinesh
Avinesh - avatar
0
my Media class is in HTML and the MediaLibrary is in CSS. https://code.sololearn.com/Wy3T5y12ueLj
3rd Nov 2019, 7:22 PM
Secr
Secr - avatar
0
@Avinesh public void addMedia(Media media) { if(media == null) { throw new IllegalArgumentException("Media can not be null"); } for(Media currMedia : this.media) { if(currMedia.getTitle().equals(currMedia.getTitle())) { this.media.remove(currMedia); } } this.media.add(media); } not sure if this one is correct
3rd Nov 2019, 7:31 PM
Secr
Secr - avatar
0
Why is this everywhere, wait I will give you an example.
3rd Nov 2019, 7:35 PM
Avinesh
Avinesh - avatar
0
@Avinesh I tried to create a test statement for that but when ever i run the test, the mediaSize() is still 3 instead of 2, since i am removing the duplicate. @Test public void testDuplicatesArePreventedFromBeingAdded() { MediaLibrary mediaLibrary = new MediaLibrary(); Media media1 = new Media("Promare - Inferno", "music", 50); Media media3 = new Media("Promare - Inferno", "music", 50); Media media2 = new Media("Overlord", "tv", 52); mediaLibrary.addMedia(media1); mediaLibrary.addMedia(media2); mediaLibrary.addMedia(media3); assertEquals(2, mediaLibrary.mediaSize(), "checking the number of media in the media list"); assertEquals(media1, mediaLibrary.getMedia().get(0)); assertEquals(media2, mediaLibrary.getMedia().get(1)); } }
3rd Nov 2019, 8:33 PM
Secr
Secr - avatar