Hello!
Is it possible to remove an object from a Set by a variable within that object? The focus of my question here is
cats.remove(Cat.name("Phantom"));
I'm receiving a compiling error that Cat symbol cannot be found, but just above this line in the code we created a HashSet with the Cat class.
Thanks! package com.codegym.task.task08.task0819;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
/*
Set of cats
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
//write your code here. step 3
cats.remove(Cat.name("Phantom"));
printCats(cats);
}
public static Set<Cat> createCats() {
//write your code here. step 2
HashSet<Cat> set1 = new HashSet<>();
set1.add(new Cat("Faust"));
set1.add(new Cat("Pig"));
set1.add(new Cat("Phantom"));
return set1;
}
public static void printCats(Set<Cat> cats) {
// step 4
for (Cat cat : cats){
System.out.println(cat.name);
}
}
// step 1
public static class Cat {
String name;
public Cat(String name){
this.name = name;
}
}
}