Question

So im trying to outprint all pets which i made. Mainly i have problem with method removeCats , i think so..., How can i change my method removeCats to let it compile correctly?

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);
    }

//2. Here i make method createCats, which must return 4 cats.

public static Set<Cat> createCats()
{

    Set<Cat> result = new HashSet<Cat>();
    result.add(new Cat());
    result.add(new Cat());
    result.add(new Cat());
    result.add(new Cat());

    return result;
}

//3. Here i make method createDogs, which must return 3 dogs.

public static Set<Dog> createDogs()
{

    Set<Dog> resultD = new HashSet<Dog>();
    resultD.add(new Dog());
    resultD.add(new Dog());
    resultD.add(new Dog());

    return resultD;
}

//4. Here i make method join, which must return all cats and dogs.

public static Set<Object> join(Set<Cat> cats, Set<Dog> dogs)
{

    Set<Object> lol = new HashSet<Object>();
    lol.add(new Cat());
    lol.add(new Dog());

    return lol;
}

//5. Method removeCats, must remove all cats

public static void removeCats(Set<Object> pets, Set<Cat> cats)
{


    Iterator<Object> it = pets.iterator();
    while(it.hasNext()){
        Object e = it.next();
             if(pets.containsAll(cats))
        pets.removeAll(cats);
    }
}

//6. Method printPets, which must outprint on screen all animals, which he contains. Every animal on new line.

public static void printPets(Set<Object> pets)
{

      for (Object s : pets){
          System.out.println(s);
      }
}

**//1. Inside class solution im making class cat and dog.**

public static class Cat{

}
public static class Dog{

}
Was it helpful?

Solution

There are several problems I see with your code:

  • Your removeCats function is creating an iterator, not using it, then probably exploding with a ConcurrentModificationException because you're removing things from the pets set while iterating over it. Many of the other answers have pointed this out, and the best way to fix this is to replace this entire function with something like pets.removeAll(cats);. If you really must use an iterator for some reason, you should be iterating over cats, and calling pets.remove(cat);

Once this is fixed, you won't crash horribly. However... removeCats will still not do what you expect, since none of the objects in cats are actually present in pets!:

  • Your join function is not doing what I'd expect it to do based on the method signature - it's not joining together the two sets, but instead discarding the inputs and creating a new list. You probably want to do something like

    pets.addAll(cats);
    pets.addAll(dogs);
    

Once you fix this issue, your printPets function will work properly, and removeCats will actually have the right effect the way it is being called, since pets will actually contain cats, and not some new instances of them.

OTHER TIPS

You can't update the Set while iterating it.Also there is no usage to iterate it.Simply remove the element if containsAll return true

 if(pets.containsAll(cats))
{
        pets.removeAll(cats);
}

There's a bug in your removeCats() method that will cause an explosion: you are directly modifying a collection while iterating over it.

You can't add or remove elements while iterating. You may only call iterator.remove();

Try this:

public static void removeCats(Set<Object> pets, Set<Cat> cats) {
    pets.removeAll(cats);
}

You cannot remove the elements from Set, while iterating over it this will give you ConcurrentModificationException

what you can use like

pets.removeAll(cats);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top