Created
January 17, 2016 15:26
-
-
Save debop/54040838aaad9a8386e9 to your computer and use it in GitHub Desktop.
using gs-collections with scala lambda expression.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GsCollectionsFunSuite extends AbstractCoreFunSuite { | |
test("FastList collection") { | |
val fastlist = FastList.newListWith[Int](1, 2, 3, 4, 5) | |
val collected: FastList[Int] = fastlist.collect(new Function[Int, Int] { | |
override def valueOf(obj: Int): Int = obj * obj | |
}) | |
collected should contain allOf(1, 4, 9, 16, 25) | |
val implicited = fastlist.collect((x: Int) => x * x) | |
implicited should contain allOf(1, 4, 9, 16, 25) | |
val allPositive: Boolean = fastlist.allSatisfy((x: Int) => x > 0) | |
allPositive shouldEqual true | |
val max = fastlist.maxBy[Integer]((x: Int) => int2Integer(x)) | |
max shouldEqual 5 | |
} | |
test("Primitive collection") { | |
val intList = IntArrayList.newListWith(1, 2, 3, 4, 5) | |
val collected: MutableList[Int] = intList.collect(new IntToObjectFunction[Int] { | |
override def valueOf(i: Int): Int = i * i | |
}) | |
collected should contain allOf(1, 4, 9, 16, 25) | |
val added: IntArrayList = IntArrayList.newListWith() | |
intList.forEach(new IntProcedure { | |
override def value(each: Int): Unit = { | |
added.add(each * each) | |
} | |
}) | |
added.toArray shouldEqual Array(1, 4, 9, 16, 25) | |
val added2: IntArrayList = IntArrayList.newListWith() | |
intList.forEach { (x:Int) => | |
added2.add(x * x) | |
() | |
} | |
added2.toArray shouldEqual Array(1, 4, 9, 16, 25) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment