How can I override the methods of the Iterator class (namely: next() , hasNext() , well, remove() ), if I created my collection that inherits AbstractCollection ? The following question arises from such inheritance: how to override the AbstractCollection methods (namely: size() , iterator() )?

It is required, of course, not only to redefine, but also so that it would be possible to work with my collection as with the standard ones!

I would be grateful for sensible tips.

  • one
    So the question is what? Why can't you just take and implement these methods? The implementation of these methods depends on your collection. - cy6erGn0m
  • Well, how to implement them so that with their help you can work normally with the collection? How to make next () return exactly the next element of the collection, hasNext () that there are more elements in the collection (at least one), etc. ???? That is the question, I said it, just not as straight as in this message! - 13th13
  • It depends on how your collection is realized. What exactly does not work? - avp
  • It is necessary to create a collection in which it is impossible to add the same objects (comparatively for some field, of course, for me, for example, by name). I created a collection, added elements to it, but when I compile, of course, the compiler curses, because you need to override the methods next (), hasNext (), and there probably will need to be both size () and iterator (). I have redefined the add () method, but I cannot throw the code, since here you have a limit on the number of characters ... (They're in such a forum) - 13th13
  • one
    I can not throw the code, because here you have a limit on the number of characters ... (They are in such a forum) Under the text of your question there is a link to " edit ." Click it and paste the code into the text of the question. To format it, select it (mouse selection) and click the button "101010" above the input form. - avp

2 answers 2

Iterator is not a class, but an interface - that is, just a declaration of methods without their implementation. AbstractCollection is an abstract class, that is, a class that has abstract (again, without implementation) methods. The size() and iterator() methods you are interested in are abstract - that is, without "meat", without implementation. Therefore, it is not a redefinition, but writing them from scratch.

The declaration of your collection should be something like this:

 public class MyCollection extends AbstractCollection { } 

As soon as you write this, then any imputed IDE itself will add the skeletons required for the implementation of the methods. Well, then write.

If you want to use some Java API developments, then you need to inherit from a class that already inherits from AbstractCollection — say, ArrayList — where all these methods are already implemented and you can use the type via super.iterator () or look at the ArrayList sources and see how everything is done

  • Thank you guys, I will now solve the problem! I have nowhere normally been answered so like you, everywhere or pour water, or do not fumble at all! It seems to be such a "life question", but could not find anything in the net! In a word, you helped out! The answers received are the most sensible! Thank you again))) - 13th13

You have two options. First, you can not make your collection, and use the usual HashSet. But then you have to modify your class (which you keep, let's call it Item) so that hashcode () and equals () work in accordance with the field on which to compare

 class Item { private String name = ""; public String getName() { return name; } public void setName(String name) { if (name == null) { throw new IllegalArgumentException(); } this.name = name; } @Override public int hashCode() { return name.hashCode(); } @Override public boolean equals(Object obj) { if (obj instanceof Item) { Item another = (Item) obj; return name.equals(another.name); } return false; } } 

The second way is to really make your collection. It can be done, for example,

 public class MySet extends AbstractSet<Item> { private Map<String, Item> c = new HashMap<String, Item>(); @Override public Iterator<Item> iterator() { return c.values().iterator(); } @Override public int size() { return c.size(); } @Override public boolean add(Item item) { if (!c.containsKey(item.getName())) { c.put(item.getName(), item); return true; } return false; } @Override public boolean contains(Object o) { if (o instanceof Item) { return c.containsKey(((Item)o).getName()); } return false; } @Override public boolean remove(Object o) { if (o instanceof Item) { c.remove(((Item) o).getName()); return true; } return false; } @Override public void clear() { c.clear(); } } class Item { private String name = ""; public String getName() { return name; } public void setName(String name) { if (name == null) { throw new IllegalArgumentException(); } this.name = name; } } 
  • Thanks for the sensible answer !!!)) - 13th13