In IntelliJ it displays two results.
package com.codegym.task.task08.task0819;
import java.util.ArrayList;
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> itr = cats.iterator();
while (cats.size() > 2) {
itr.next();
itr.remove();
}
printCats(cats);
}
public Solution() {
}
public static Set<Cat> createCats() {
HashSet<Cat> catsSet = new HashSet<>();
Cat lizzie = new Cat();
Cat bill = new Cat();
Cat meow = new Cat();
catsSet.add(lizzie);
catsSet.add(bill);
catsSet.add(meow);
return catsSet;
}
public static void printCats(Set<Cat> cats) {
ArrayList<Object> catsList = new ArrayList<>(cats);
for (int i = 0; i < catsList.size(); i++) {
System.out.println(catsList.get(i));
}
}
public static class Cat {
public Cat() {
}
}
}