设为首页 加入收藏

TOP

Scala For Java的一些参考(二)
2017-10-10 12:13:02 】 浏览:2702
Tags:Scala For Java 一些 参考
Pounds = wallet.get("GBP")

val updatedWallet = wallet + ("GBP" -> 20)  新的不可变

val tenDollars = "USD"-> 10 Tuple

   

def increment = (x:Int) => x + 1

List(1,2,3,4).map(increment)

List(1,2,3,4) map increment

String Interpolation

val many = 10000.2345

val amount = s"$many euros"

格式化宽度

val amount = f"$many%12.2f euros"

val amount = s"${many*2} euros"

val printedAmounts = amounts map(m=> s"${m.amount} ${m.currency}")

groupBy

groupBy method that transforms a collection into a  Map collection:

val sortedAmounts = amounts groupBy(_.currency)

Scala UnitTest

POM.xml maven依赖文件

?  Dependency for the core scala-library:

<dependency>

<groupId>org.scala-lang</groupId>

<artifactId>scala-library</artifactId>

<version>2.10.0</version>

</dependency>

?  Dependency for scalatest (a framework for testing in Scala that supports

JUnit and other styles; we will cover it in detail in Chapter 4, Testing Tools):

<dependency>

<groupId>org.scalatest</groupId>

<artifactId>scalatest_2.10</artifactId>

<version>2.0/version>

<scope>test</scope>

</dependency>

?  Dependency for JUnit to use Java  Assert statements in our test case:

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

scala-maven-plugin

<plugin>

<groupId>net.alchim31.maven</groupId>

<artifactId>scala-maven-plugin</artifactId>

<executions>

<execution>

<id>scala-compile-first</id>

<phase>process-resources</phase>

<goals>

<goal>add-source</goal>

<goal>compile</goal>

</goals>

</execution>

<execution>

<id>scala-test-compile</id>

<phase>process-test-resources</phase>

<goals>

<goal>testCompile</goal>

</goals>

</execution>

</executions>

</plugin>

单元测试代码

import org.junit._

import Assert._

class CustomerScalaTest {

  @Before

  def setUp: Unit = {

  }

  @After

  def tearDown: Unit = {

  }

  @Test

  def testGetCustomerId = {

    System.out.println("getCustomerId")

    val instance = new Customer()

    val expResult: Integer = null

    val result: Integer = instance.getCustomerId()

    assertEquals(expResult, result)

  }

}

Java/Scala Collection转换

./activator console

import java.util.Arrays

val javaList = Arrays.asList(1,2,3,4)

import scala.collection.JavaConverters._

val scalaList = javaList.asScala

val javaListAgain = scalaList.asJava

assert( javaList eq javaListAgain)

JavaBean-style properties

class Company(var name:String)

val sun = new Company("Sun Microsystems")

sun.name

sun.name_=("Oracle")

import scala.beans.BeanProperty

class Company(@BeanProperty var name:String)

val sun = new Company("Sun Microsystems")

sun.getName()

sun.setName("Oracle")

OO

class Customer ( var customerId

首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇函数式非凡的抽象能力 下一篇LINUX系统下Java和Scala的环境配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目