ΠΠΎΠ³Π΄Π° Π·Π°ΠΏΡΡΠΊΠ°Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, Π²ΡΡ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ, Π²ΡΠΎΠ΄Π΅, ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ.
ΠΠ°Π»ΠΈΠ΄Π°ΡΠΎΡ Π²ΡΠΊΠΈΠ΄ΡΠ²Π°Π΅Ρ ΠΎΡΠΈΠ±ΠΊΠΈ:
-ΠΠ½ΡΡΡΠΈ ΠΊΠ»Π°ΡΡΠ° Solution Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ public static ΠΊΠ»Π°ΡΡΡ Cat, Dog;
-ΠΠ΅ΡΠΎΠ΄ join() Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½Π΅Π½Π½ΠΎΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π²ΡΠ΅Ρ
ΠΆΠΈΠ²ΠΎΡΠ½ΡΡ
;
-ΠΠ΅ΡΠΎΠ΄ removeCats() Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠ΄Π°Π»ΡΡΡ ΠΈΠ· ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° pets Π²ΡΠ΅Ρ
ΠΊΠΎΡΠΎΠ²;
-ΠΠ΅ΡΠΎΠ΄ printPets() Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡ Π½Π° ΡΠΊΡΠ°Π½ Π²ΡΠ΅Ρ
ΠΆΠΈΠ²ΠΎΡΠ½ΡΡ
, ΠΊΠΎΡΠΎΡΡΠ΅ Π² Π½Π΅ΠΌ Π΅ΡΡΡ. ΠΠ°ΠΆΠ΄ΠΎΠ΅ ΠΆΠΈΠ²ΠΎΡΠ½ΠΎΠ΅ Ρ Π½ΠΎΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ.
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> cats = new HashSet<Cat>();
cats.add(new Cat("Muska"));
cats.add(new Cat("Iriska"));
cats.add(new Cat("Murka"));
cats.add(new Cat("Kuzya"));
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return cats;
}
public static Set<Dog> createDogs() {
HashSet<Dog> dogs = new HashSet<>();
dogs.add(new Dog("Timka"));
dogs.add(new Dog("Karina"));
dogs.add(new Dog("Tosha"));
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return dogs;
}
public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs) {
Set<Object> joinpets = new HashSet<>();
joinpets.addAll(cats);
joinpets.addAll(dogs);
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return joinpets;
}
public static void removeCats(Set<Object> pets, Set<Cat> cats) {
pets.removeAll(cats);
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
}
public static void printPets(Set<Object> pets) {
for (Object dog : pets)
System.out.println(dog.toString());
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
}
public static class Cat {
private String name;
public Cat(String name){
this.name = name;
}
@Override
public String toString() {
String name = this.name;
return name;
}
}
public static class Dog {
private String name;
public Dog(String name){
this.name = name;
}
@Override
public String toString() {
String name = this.name;
return name;
}
}
package com.javarush.task.task08.task0820;
import java.util.*;
/*
ΠΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π²ΡΠ΅Ρ
ΠΆΠΈΠ²ΠΎΡΠ½ΡΡ
*/
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> cats = new HashSet<Cat>();
cats.add(new Cat("Muska"));
cats.add(new Cat("Iriska"));
cats.add(new Cat("Murka"));
cats.add(new Cat("Kuzya"));
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return cats;
}
public static Set<Dog> createDogs() {
HashSet<Dog> dogs = new HashSet<>();
dogs.add(new Dog("Timka"));
dogs.add(new Dog("Karina"));
dogs.add(new Dog("Tosha"));
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return dogs;
}
public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs) {
Set<Object> joinpets = new HashSet<>();
joinpets.addAll(cats);
joinpets.addAll(dogs);
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
return joinpets;
}
public static void removeCats(Set<Object> pets, Set<Cat> cats) {
//Set<Object> removeCats = new HashSet<>(pets);
pets.removeAll(cats);
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
}
public static void printPets(Set<Object> pets) {
for (Object dog : pets)
System.out.println(dog.toString());
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
}
public static class Cat {
private String name;
public Cat(String name){
this.name = name;
}
@Override
public String toString() {
String name = this.name;
return name;
}
}
public static class Dog {
private String name;
public Dog(String name){
this.name = name;
}
@Override
public String toString() {
String name = this.name;
return name;
}
}
//Π½Π°ΠΏΠΈΡΠΈΡΠ΅ ΡΡΡ Π²Π°Ρ ΠΊΠΎΠ΄
}