これは正常にコンパイルされますが、実行時に次のように爆発します。
スレッド"main"の例外java.lang.NoSuchMethodError:scala.collection.immutable.List.filter(Lscala / Function1;)Lscala / collection / immutable / List
import scala.Function1;
import scala.collection.immutable.List;
import scala.collection.immutable.Nil$;
import scala.runtime.AbstractFunction1;
public class FunProc {
List nil = Nil$.MODULE$; // the empty list
List<Integer> list1 = nil.$colon$colon(1); // append 1 to the empty list
List<Integer> list2 = list1.$colon$colon(2); // append 2 to List(1)
List<Integer> list3 = list2.$colon$colon(3).$colon$colon(14).$colon$colon(8); // List(8, 14, 3, 2, 1)
Function1<Integer, Object> filterFn = new AbstractFunction1<Integer, Object>() {
public Boolean apply(Integer value) { return value<10; }
};
List<Integer> list4 = list3.filter(filterFn); // List(8, 3, 2, 1)
public void doIt() {
System.out.println("Filtered List is " + list4);
}
}
編集
idonnieの答えを試した後、私はこれを思いついた:
List<Integer> list4 = list3.toTraversable().filter(filterFn).toList();
これは、キャストの代わりに変換が使用されることを除いて、基本的にidonnieの回答と同じです。次のコンパイルが正常に行われることを考えると、toTraversable()が必要な理由を知りたいと思います。
List<Integer> list4 = list3.filter(filterFn);