• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to use the method iterator()?

Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening!

I'm still learning java and tonight I'm stumbling over the way my teacher used the iterator method of the interface Collection.

Indeed, my teacher defined such a following value method of a class of elements HashConcret:

I don't see any elements being passed as arguments, entering it the iterator... to have their value being moved up to the sum!
So does it actually work? And if it does, how?

[table="width: 500, class: outer_border"]
[td]Class Composant is abstract and defined the characteristics of a named given composant. It is characterized by a name and realize two interfaces Named and Valuable defined below. Two composants are equals if they have same name and same value.

Class Atome is a composant characterized by a name and a double value. An atome is equal to another atome if they have same value. The name of an atome is not given at creation. it is formed by its parent's name. followed by "_" and its value.

Abstract class Composite is defined by the characteristics of a complex composant having a collection of named elements of type Atome or Coposite. The class composite forbidden dupplicates. It realizes the interface ComposantManagement. Two objects are equals if they have same name and same elements.

Class HashConcret is a subclass of Composite which doesn't allow to have duplications and which elements aren't sorted out. The value of an instance of HashConcret is the mean of the value of its elements.[/td]

this description corresponds to the following code: HashConcret is at the bottom!

I want to understand the method value() of class Composite.
Posts: 3063
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Antoine, I'm moving your question over to our Java In General forum. The JForum forum is really for questions about the JForum application, which manages the forums here at Java Ranch, and many other sites as well.

You're right that the "i" variable of your for loop doesn't have any effect on the iterator since, as you pointed out, it's not passed to it as a parameter. In fact, it's more common to see an iterator used in a while loop. Basically, you ask it via the hasNext() method if there are any more elements to look at, and then you get the next element with the next() method. I see that you are trying to iterate over a Set called elements. I'm not sure what the assignment is, but you could ask that set to create an iterator for you. If your assignment is to write your own iterator, you could create a class that implements the Iterator interface and is instantiated with the Set elements.
Posts: 79390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why not use one of the ready made List implementations and use its Iterator? Or better still, a Stream (Java8 only).
The stream method creates a Stream object which iterates the contents of the List; since it “knows” the List contains Expiables, it will be a Stream of Expiables.
The map to double method creates a stream of doubles and the bit with -> in takes an argument (ex for an Expiable) and then goes to its value method which appears to return a double.
Once you have a stream of doubles, you can use its sum method whose intent should be obvious from its name.

If you are going to use an Iterator explicitly, don't make it a plain simple Iterator. Call it an Iterator<Valuable> then you son't have to mess about with casts.
    Bookmark Topic Watch Topic
  • New Topic