Help me with this
Movie{
int id;
String title;
}
List<Movie> movies = new ArrayList<Movie>();
Movie movie1 = new Movie(1,"movie1");
Movie movie2 = new Movie(2,"movie2");
Movie movie3 = new Movie(1,"movie3");
Movie movie4 = new Movie(2,"movie4");
movies.add(movie1); movies.add(movie2); movies.add(movie3); movies.add(movie4);
Now I have a list of movies including all 4 of above.
(movies1,movies2,movies3,movies4)
But I want my list of movies contain only the last movies added among the ones that have the same Id which is :
(movies3,movies4);
Update : Thank @LeffeBrune for the answer but now if I want two or more fields, not just one. What should I do?
Movie{
int id; String title ; String plot;
}
for example for both id and title field.
(1,"title1","plot1"),(2,"title2","plot2"),(1,"title3","plot3"),(1,"title1","plot4")
will become
(2,"title2","plot2"),(1,"title3","plot3"),(1,"title1","plot4"),
Based on LeffeBrune's answer, Should I put the whole movie object as key and override an equal method.
movie1andmovie3frommoviesitself, or just return a list of movies that hasid=2? Your question indicates you want them removed, but then what is the point of adding them in the first place if we're going to never need them again?