Help please
package com.codegym.task.task08.task0820;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
/*
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());
//write your code here
return result;
}
public static Set<Dog> createDogs() {
//write your code here
HashSet<Dog> result = new HashSet<Dog>();
result.add(new Dog());
result.add(new Dog());
result.add(new Dog());
return result;
}
public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs) {
//write your code here
Set<Object> pets = new HashSet<>();
Iterator<Cat> catIterator = cats.iterator();
Iterator<Dog> dogIterator = dogs.iterator();
while(catIterator.hasNext()) {
pets.add(catIterator.next());
}
while(dogIterator.hasNext()) {
pets.add(dogIterator.next());
}
return pets;
}
public static void removeCats(Set<Object> pets, Set<Cat> cats) {
//write your code here
HashSet<Object> temp = new HashSet<>(pets);
Iterator<Object> petsIterator = temp.iterator();
Iterator<Cat> catIterator = cats.iterator();
Object pet;
Cat cat;
while(petsIterator.hasNext()) {
pet = petsIterator.next();
while(catIterator.hasNext()) {
cat = catIterator.next();
if(pet.equals(cat)) {
pets.remove(cat);
}
}
}
}
public static void printPets(Set<Object> pets) {
//write your code here
for(Object pet : pets)
System.out.println(pet);
}
public static class Cat{
public Cat() {
}
}
public static class Dog{
public Dog() {
}
}
//write your code here
}
printPets
twice. So first seven lines of output is the whole set of pets. The last three are only Dog class objects, after removing Cat objects.