Hey i think i have some logic problem, but yet i cant really see where it is. I know that the Problem is somewhere between HashSet, but thats it.
I am using a HashMap and the key is numberOfEpisode and the values are HashSet. So if these animes in my List of Array has the same amount of Episode, like 12 then add them to a new HashSet and then into the HashMap.
Code
HashSet<Strings> animeNames;
for(Anime anime : animius.getListOfAnime){
animeNames = new HashSet<>();
for(Anime anime2 : animius.getListOfAnime){
for(Episode episode : anime.getListOEpisode){
for(Episode episode2 : anime2.getListOfEpisode{
if(episode.getNumber == episode2.getNumber){
animeNames.add(anime.getName);
animius.getEpisodeAndAnimeIndex.put(episode.getNumber, animeNames)
}
}
}
}
}
Output
After Formatting it. And Lets say only anime1 has similarities towards anime 2, but anime 2 has 2 Keys one with 12 Episode and the other one with 24 Episode.
12 ---- anime1, anime2 <--- this is correct
24 ---- anime1, anime2 <--- this is wrong
Expected Output
12 ---- anime1, anime2
24 ---- anime2
Attempted Solutions
I have created 2 HashSet with one which contains the values of anime.getName and the other one which contains anime2.getName.
I also played around with putting the new instantiation of animeNames and animeNames2 differently in the for each loops
If I haven't misunderstood you, you don't need to have 4 nested for loops. You just want to check for each anime in your collection what number of episodes they have and store that. Essentially group animes together by number of episodes. The code below does that.
HashMap<Integer, Set<Anime>> animeMap = new HashMap<>();
for(Anime anime : animius.getListOfAnime){
int numberOfEpisodes = anime.getListOEpisodes().size();
if (animeMap.contains(numberOfEpisodes) {
animeMap.get(numberOfEpisodes).add(anime);
} else {
HashSet<Anime> animeNames = new HashSet<>();
animeNames.add(anime);
animeMap.put(numberOfEpisodes, animeNames);
}
}