scala+Java7来模拟函数式编程
由于项目一直使用Java7开发,所以无法直接升Java8。之后接触了scala进行几个月的Spark计算方面的开发,回过头来一看Java7的代码太,,,,难以直视了。一度不想在用Java开发了。
一次偶然的机会我试了一下scala程序打包,放到Java端调用,结果百试不爽,将一些工具包通过简洁的scala开发然后打包到Java端,效率直接飞了。随后想到,何不将Java、scala结合起来,取二者长处(当然这里针对用老Java开发的平台并且难以转型的项目)来进行开发呢?于是有了以下的测试代码:这里代码只是示范性的写了一些常用List和Map的方法,其他功能等之后有时间再继续弄
- scala程序
package s4j.scala.utils4j
import scala.collection.JavaConverters._
/**
* 自定义列表
* @tparam U
*/
class ExList[U](){
private var _li : List[U] = Nil
def clear : ExList[U] = {
_li = Nil
this
}
def fill(n : Int,elem : U) : ExList[U] = {
_li = List.fill(n)(elem)
this
}
def union(that : ExList[U]) : ExList[U] = {
_li :::= that._li
this
}
def append (elem : U) : ExList[U] = {
_li :+= elem
this
}
def delete (elem : U) : ExList[U] = {
_li.drop( _li.indexOf( elem ) )
this
}
def sort[Double] : ExList[U] ={
val v = _li(0).getClass.getSimpleName
v match {
case "Integer"|"Double"|"Byte"|"Short"|"Long"|"Char"|"Float" =>
_li = _li.sortWith((a,b)=> java.lang.Double.parseDouble(a.toString) < java.lang.Double.parseDouble(b.toString) )
case "String"=>
_li = _li.sortWith((a,b)=>a.toString < b.toString)
case _ =>
}
this
}
def map2Str : ExList[U] = {
_li.map(_.toString)
this
}
def addFromJava(li : java.util.List[U]): ExList[U] ={
_li = li.asScala.toList
this
}
def addFromJava(arr : Array[U] ): ExList[U] ={
_li = arr.toList
this
}
def map(body : Function[U]) : ExList[U] = {
_li = _li.map(body.run)
this
}
def get(n : Int) : U = _li(n)
def mkString(str : String):String = _li.mkString(str)
override def toString: String = "["+_li.map(_.toString).mkString(",")+"]"
/**
* 获得索引到元素的映射
* @return
*/
def mapWithIndex = new ExMap[Int,U].from(_li.zipWithIndex.map{case(k,v)=>(v,k)})
def zip[T] (other : ExList[T]) : ExMap[U,T] = new ExMap().from(_li zip other._li)
def contains(elem : U):Boolean = _li.contains(elem)
def indexOf(elem : U):Int = _li.indexOf(elem)
def print = {
println(_li.map(_.toString))
this
}
def asJava = _li.asJava
def forEach(body : Function[U]): Unit =_li.foreach(body.run)
}
object ExList{
def numList(min : Int , max : Int , step : Int) : ExList[Int] = new ExList[Int].addFromJava((min to max).by(step).toArray)
}
/**
* 自定义映射
* @tparam U
* @tparam T
*/
class ExMap[U,T]{
private var _map : Map[U,T] = Map()
def clear :ExMap[U,T]={
_map = Map()
this
}
def add (k:U,v:T):ExMap[U,T]={
_map += (k -> v)
this
}
def union(that : ExMap[U,T]) :ExMap[U,T]={
_map ++= that._map
this
}
def delete(k : U) :ExMap[U,T]={
_map -= k
this
}
def get(k : U,dflt : T) : T = _map.getOrElse(k , dflt)
def contains(k : U) : Boolean = _map.contains(k)
def keys = _map.keys.toList.asJava
def values = _map.values.toList.asJava
def print : ExMap[U,T] = {
println(_map)
this
}
def asJava = _map.asJava
def from(li : List[(U,T)]) : ExMap[U,T] = {
_map = li.toMap
this
}
}
trait Function[U]{ self =>
def run(elem : U) : U
}
- Java调用
import s4j.scala.utils4j.ExList;
import s4j.scala.utils4j.ExMap;
import s4j.scala.utils4j.Function;
public class Demo {
final static Object func(Object o){
return (int)o + 1;
}
public static void main(String[] args){
ExList.numList(1,10,1)
.map(new Function<Object>() {
@Override
public Object run(Object elem) {
return func(elem);
}
}).print()
.map(new Function<Object>() {
@Override
public Object run(Object elem) {
return new ExList<Object>().append(elem);
}
}).print()
.map(new Function<Object>() {
@Override
public Object run(Object elem) {
return ((ExList)elem).zip( ((ExList)elem) );
}
})
.forEach(new Function<Object>() {
@Override
public Object run(Object elem) {
return ((ExMap)elem).print();
}
});
;
}
}
运行输出如下:
D:\java\jdk1.7\bin\java ...
List(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
List([2], [3], [4], [5], [6], [7], [8], [9], [10], [11])
Map(2 -> 2)
Map(3 -> 3)
Map(4 -> 4)
Map(5 -> 5)
Map(6 -> 6)
Map(7 -> 7)
Map(8 -> 8)
Map(9 -> 9)
Map(10 -> 10)
Map(11 -> 11)
Process finished with exit code 0