Hi there,
I have tried to figure it out but I am not sure why my code is not working properly. The remove Cats() method does not work. Does anybody have an idea how to solve this?
Thanks.
package com.codegym.task.task08.task0820;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
Animal set
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
Set<Dog> dogs = createDogs();
Set<Object> pets = join(cats, dogs);
printPets(pets);
removeCats(pets, cats);
printPets(pets);
}
public static Set<Cat> createCats() {
HashSet<Cat> result = new HashSet<Cat>();
result.add(new Cat ());
result.add(new Cat ());
result.add(new Cat ());
result.add(new Cat ());
return result;
}
public static Set<Dog> createDogs() {
HashSet<Dog> set2 = new HashSet<>();
set2.add(new Dog());
set2.add(new Dog());
set2.add(new Dog());
return set2;
}
public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs) {
HashSet pets = new HashSet();
pets.addAll(cats);
pets.addAll(dogs);
return pets;
}
public static void removeCats(Set<Object> pets, Set<Cat> cats) {
for (Cat cat:cats)
{
pets.remove(cats);
}
}
public static void printPets(Set<Object> pets) {
Iterator <Object> itr = pets.iterator();
while (itr.hasNext())
{
System.out.println(itr.next());
}
}
public static class Dog {
}
public static class Cat {
}
}