package com.javarush.task.task08.task0820; import java.util.HashSet; import java.util.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); System.out.println(pets.size()); } public static Set<Cat> createCats() { HashSet<Cat> result = new HashSet<Cat>(); result.add(new Cat("1")); result.add(new Cat("2")); result.add(new Cat("3")); result.add(new Cat("4")); return result; } public static Set<Dog> createDogs() { HashSet<Dog> result = new HashSet<Dog>(); result.add(new Dog("5")); result.add(new Dog("6")); result.add(new Dog("7")); return result; } public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs) { Set<Object> set = new HashSet<Object>(); set.addAll(cats); set.addAll(dogs); return set; } public static void removeCats(Set<Object> pets, Set<Cat> cats) { pets.removeAll(cats); } public static void printPets(Set<Object> pets) { for (Object o : pets) { System.out.println(o); } } public static class Cat{ String name; public Cat() { } public Cat(String name) { this.name = name; } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + '}'; } } public static class Dog{ String name; public Dog() { } public Dog(String name) { this.name = name; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + '}'; } } } ВЫВОД Cat{name='2'} Dog{name='5'} Dog{name='7'} Cat{name='4'} Dog{name='6'} Cat{name='1'} Cat{name='3'} Dog{name='5'} Dog{name='7'} Dog{name='6'} 3 Process finished with exit code 0