DEV Community

Hyunjin Cho
Hyunjin Cho

Posted on

[Android Studio] Duplicate value

Three arrayList:
Two for ArrayList - will take information of movie
One for ArrayList - will take movie id of the movie

  1. Add all movie information object from database to reviewedArrayList(ArrayList)

  2. While looping the reviewedArrayList, check if the movieIDArrayList(ArrayList) contains the movie id. When it does not have it, add it to movieIDArrayList and add the information object containing the movie id to newList(ArrayList) and if not, skip it.

  3. Give newList which does not have duplicated value(movie id) to somewhere that I want to use.

  public void onDataChange(@NonNull DataSnapshot snapshot) {
                reviewedArrayList = new ArrayList<>();
                movieIDArrayList = new ArrayList<>();
                newList = new ArrayList<>();
                for(DataSnapshot childSnapshot: snapshot.getChildren()){
                    Playlist playlist = childSnapshot.getValue(Playlist.class);
                    reviewedArrayList.add(playlist);

                    for(int i=0; i < reviewedArrayList.size(); i++){
                        String mID = reviewedArrayList.get(i).mID;
                        if(!movieIDArrayList.contains(mID)){
                            movieIDArrayList.add(mID);
                            newList.add(reviewedArrayList.get(i));
                        }
                    }

                }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)