Scala / Java Commons
Iterable vs Iterator
public interface Iterable<T> { Iterator<T> iterator(); }
An iterator is a simple way of allowing some to loop through a collection of data without assignment privileges (though with ability to remove).
public interface Iterator<E> { boolean hasNext(); E next(); void remove(); }
Iterable does not track current position, iterator keeps track of current location in a collection
Iterator provides methods hasNext() and next()
Once an iterator is looped through by using foreach, it cannot be reused
Iterable supports for(T item:items) syntax
If you want to loop through a collection, called iterable.iterator for each looping
Iterator in scala extends TraversableOnce trait, Iterable does not
Find class member variables
case class StockType(date: java.sql.Date, open: Double, high: Double, low: Double, close: Double, volume: Double, adjclose: Double, symbol: String)
object Sample {
def getFields[T](implicit tag: Manifest[T] ) = {
(tag.runtimeClass).getDeclaredFields.map(f => (f.getName, f.getType))
}
def main(args:Array[String]){
getFields[StockType].foreach(println)
}
}
Output
(date,class java.sql.Date)
(open,double)
(high,double)
(low,double)
(close,double)
(volume,double)
(adjclose,double)
(symbol,class java.lang.String)