Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding remove method of an iterator

 
Rachit Pant
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing iterator method of a class.
The structure of class is as:

class A{
TypeX[] typex;
public Iterator<TypeX> iterator() {} // this is the method i wanna write
}

I tried this in my iterator methods code:

return Arrays.asList(typex).iterator();

This will return the Iterator<TypeX> as required , but i want my iterator to throw an UnsupportedOperationException, when i call its remove method.
So I need to override this method.
Can someone tell me how to do this??

 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachit Pant wrote:I am writing iterator method of a class.
The structure of class is as:

class A{
TypeX[] typex;
public Iterator<TypeX> iterator() {} // this is the method i wanna write
}

I tried this in my iterator methods code:

return Arrays.asList(typex).iterator();

This will return the Iterator<TypeX> as required , but i want my iterator to throw an UnsupportedOperationException, when i call its remove method.
So I need to override this method.
Can someone tell me how to do this??




There is no black magic here... The easiest way to override a method, is to actually override the method. This means that you need access to the super class to inherit from. And since you don't, you can either (1) write a wrapper iterator or better yet (2) write an iterator (there is only three methods) that iterates your data structure.

Henry
 
Rachit Pant
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write small piece of code to demonstrate this. I wanna learn both these ways . Or some pointers please.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Iterator returned by Arrays.asList(typex).iterator() already throws an UnsupportedOperationException when remove() is called. The List is not read-only, but its size cannot be changed. That means it's not possible to add or remove elements, only replace existing elements. In other words, you don't need to do anything in this case.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note, though, that if this particular Iterator didn't already do what you wanted, you would have to find a different approach, such as writing your own Iterator implementation from scratch, because the implementations in the Core API Collections Framework are private and cannot be overridden.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can wrap the existing Iterator into a new Iterator. The hasNext() and next() methods are built on top of the return values of the original Iterator. An example, from AbstractMap's keySet() method:
This iterator() implementation wraps the Iterator returned by entrySet().iterator(), no matter what that returns (that depends on the implementation from HashMap, TreeMap, etc). You could do something similar but let remove() throw an UnsupportedOperationException instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic