Difference between revisions of "Iterable Immutable List Assignment"
Jump to navigation
Jump to search
Line 44: | Line 44: | ||
=Diagrams= | =Diagrams= | ||
+ | ==ImmutableList== | ||
+ | |||
+ | <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); <nowiki> | ||
+ | |||
[[File:ImmutableList_letters.svg|600px]] | [[File:ImmutableList_letters.svg|600px]] | ||
+ | |||
+ | ==iterator()== | ||
+ | |||
+ | <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); | ||
+ | Iterator<Char> iter = letters.iterator() <nowiki> | ||
[[File:ImmutableList_iterator.svg|600px]] | [[File:ImmutableList_iterator.svg|600px]] | ||
+ | |||
+ | ==next()== | ||
[[File:ImmutableList_iterator_next.svg|600px]] | [[File:ImmutableList_iterator_next.svg|600px]] | ||
+ | |||
+ | ==next() next()== | ||
[[File:ImmutableList_iterator_next_next.svg|600px]] | [[File:ImmutableList_iterator_next_next.svg|600px]] | ||
+ | ==next() next() next()== | ||
[[File:ImmutableList iterator next next next.svg|600px]] | [[File:ImmutableList iterator next next next.svg|600px]] | ||
Revision as of 03:55, 19 December 2020
Contents
Studio To Revisit
In this studio we will complete the DefaultImmutableList
implementation from the ImmutableList studio.
ImmutableList
The ImmutableList interface extends Iterable which has one method: iterator().
public interface ImmutableList<E> extends Iterable<E> { E head(); ImmutableList<E> tail(); boolean isEmpty(); }
Iterable
interface java.lang.Iterable<T>
Iterator
interface java.util.Iterator<T>
Examples
Using Iterable
Implementing Iterable
DoubleRange
ArrayIterable
Example
class: | IterableImmutableListExample.java | |
package: | immutable.list.example | |
source folder: | src/main/java |
The code:
ImmutableList<Integer> numbers = Lists.brackets(4, 66, 99); for (Integer i : numbers) { System.out.println(i); }
produces:
4 66 99
Diagrams
ImmutableList
ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); <nowiki> [[File:ImmutableList_letters.svg|600px]] ==iterator()== <nowiki>ImmutableList<Char> letters = Lists.brackets('A', 'B', 'C'); Iterator<Char> iter = letters.iterator() <nowiki> [[File:ImmutableList_iterator.svg|600px]] ==next()== [[File:ImmutableList_iterator_next.svg|600px]] ==next() next()== [[File:ImmutableList_iterator_next_next.svg|600px]] ==next() next() next()== [[File:ImmutableList iterator next next next.svg|600px]] =Code To Implement= ==enum EmptyImmutableList== {{JavaToImplement|EmptyImmutableList|iterator|immutable.list.assignment}} ===iterator()=== return an [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator] for the EmptyImmutableList. Question to ask yourself: Does an iterator for an EmptyImmutableList ever have a next? ==class NonEmptyImmutableList== {{JavaToImplement|NonEmptyImmutableList|iterator|immutable.list.assignment}} ===iterator()=== return an [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator] for this instance of NonEmptyImmutableList. Note: If you are within an anonymous inner class, the way to get the outer class's this instance is to qualify it with the out class identifier. For example: <nowiki>NonEmptyImmutableList.this
Test
class: | IterableAssignmentTestSuite.java | |
package: | immutable.list.assignment | |
source folder: | src/test/java |