Hi,
1) I don't understand why cat1 in line 19 can't be resolved to a variable (the message I get from my IDE).
2) Is there another way to remove a specific cat from the set, without using a field of name? Why can't I just use the object reference?
3) I also tried in line 20: iterator.remove(cat1) - but doesn't work either..
Anyone, who can explain and help?
Thank you! :)
package com.codegym.task.task08.task0819;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
Set of cats
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
Iterator<Cat> iterator = cats.iterator();
while (iterator.hasNext()) {
Cat cat = iterator.next();
if (cat.equals(cat1)) {
iterator.remove();
}
}
printCats(cats);
}
public static Set<Cat> createCats() {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
Set<Cat> cats = new HashSet<>();
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
return cats;
}
public static void printCats(Set<Cat> cats) {
for (Cat cat : cats) {
System.out.println(cat);
}
}
public static class Cat {
}
}