As in the title - when I run the code in the Intellij I got two objects (of the three originally).
Hopefully, if more wise eyes look into the code, the better chance to see where is the mistake :)
Thank you^^
package com.codegym.task.task08.task0819;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/* 1. Inside the Solution class, create a public static Cat class.
2. Implement the createCats method. It must create a Set of cats and add 3 cats to it.
3. In the main method, remove one cat from Set cats.
4. Implement the printCats method. It should display all the cats that remain in the set.
Each cat on a new line.
Requirements:
1. The program should display text on the screen.
2. Inside the Solution class, there must be a public static Cat class with a default constructor.
3. The Solution class's createCats() method must return a Set containing 3 cats.
4. The Solution class's printCats() method must display all the cats in the set. Each cat on a new line.
5. The main() method should call the createCats() method once.
6. The main() method should call the printCats() method.
7. The main() method must remove one cat from the set of cats.
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
for (Iterator<Cat> iterator = cats.iterator(); iterator.hasNext(); ) {
Cat next = iterator.next();
if (next.name == "cat1"){
iterator.remove();
break;}
}
printCats(cats);
}
public static Set<Cat> createCats () {
HashSet<Cat> catSet = new HashSet<Cat>();
Cat cat1 = new Cat();
cat1.name = "cat1";
Cat cat2 = new Cat();
cat2.name = "cat2";
Cat cat3 = new Cat();
cat3.name = "cat3";
catSet.add(cat1);
catSet.add(cat2);
catSet.add(cat3);
return catSet;
}
public static void printCats (Set < Cat > cats) {
for (Cat cat: cats) {
System.out.println(cat);
}
}
public static class Cat {
public String name;
}
}