Scala For Java的一些參考

来源:http://www.cnblogs.com/2018/archive/2016/04/08/5366541.html
-Advertisement-
Play Games

變數 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java Programmer" val yourPast = "Good Java Programmer" var yourFuture = "Go ...


     

變數

String yourPast = "Good Java Programmer";

val yourPast : String = "Good Java Programmer"

val yourPast = "Good Java Programmer"

var yourFuture = "Good Java Programmer"

自動類型推斷;可變、不可變變數

class Money(amount:Int) amount不是成員變數

class Money(val amount:Int)

val notMuch = new Money(2)

notMuch.amount

class Money(var amount:Int)

val notMuch = new Money(2)

notMuch.amount=3

case classes

public class Money {

private Integer amount;

private String currency;

public Money(Integer amount, String currency) {

this.amount = amount;

this.currency = currency;

}

public Integer getAmount() {

return amount;

}

public void setAmount(Integer amount) {

this.amount = amount;

}

public String getCurrency() {

return currency;

}

public void setCurrency(String currency) {

this.currency = currency;

}

@Override

public int hashCode() {

int hash = 5;

hash = 29 * hash + (this.amount != null ? this.amount.

hashCode() : 0);

hash = 29 * hash + (this.currency != null ? this.currency.

hashCode() : 0);

return hash;

}

@Override

public boolean equals(Object obj) {

if (obj == null) {

return false;

}

if (getClass() != obj.getClass()) {

return false;

}

final Money other = (Money) obj;

return true;

}

@Override

public String toString() {

return "Money{" + "amount=" + amount + ", currency=" +

currency + '}';

}

public Money add(Money other) {

return new Money(this.amount +

other.amount, this.currency);

}

}

case class Money(amount:Int=1, currency:String="USD")  two immutable fields the fields declared in Scala classes are public

case class Money(private val amount: Int, private val currency: String)

to make them private instead, or used  var instead of  val to make the fields mutable.

val defaultAmount = Money()

val fifteenDollars = Money(15,"USD")

val fifteenDollars = Money(15)

val someEuros = Money(currency="EUR")

case class Money(val amount:Int=1, val currency:String="USD"){

def +(other: Money) : Money = Money(amount + other.amount)

}

Money(12) + Money(34)

collections

Scala collections are, by default, immutable

val numbers = List(1,2,3,4,5,6)

val reversedList = numbers.reverse

val onlyAFew = numbers drop 2 take 3

cons operator

val numbers = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil

val simpleList = Nil.::(6)

val twoElementsList = List(6).::(5)

列表串接

val concatenatedList = simpleList ::: twoElementsList

val things = List(0,1,true) AnyVal

val things = List(0,1,true,"false") Any

複雜對象列表

val amounts = List(Money(10,"USD"),Money(2,"EUR"),Money(20,"GBP"),

Money(75,"EUR"),Money(100,"USD"),Money(50,"USD"))

Filter

val euros = amounts.filter(money => money.currency=="EUR")

val euros = amounts.filter(x => x.currency=="EUR")

val euros = amounts.filter(_.currency=="EUR")

partition

val allAmounts = amounts.partition(amt => amt.currency=="EUR")

Tuples

val euros = allAmounts._1

val everythingButEuros= allAmounts._2

val (euros,everythingButEuros) = amounts.partition(amt => amt.currency=="EUR")

Map

Map amounts = new HashMap<String,Integer>();

amounts.put("USD", 10);

amounts.put("EUR", 2);

val wallet = Map( "USD" -> 10, "EUR" -> 2 )

val someEuros = wallet("EUR")

Option類型

val mayBeSomePounds = 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: Int, var zip: String) {

def this( zip: String) = this(0,zip)

def getCustomerId() = customerId

def setCustomerId(cust: Int): Unit = {

customerId = cust

}

}

val customer = new Customer("123 45")

traits

interface VIPCustomer {

Integer discounts();

}

class Customer(val name:String, val discountCode:String="N" ){

def discounts() : List[Int] = List(5)

override def toString() = "Applied discounts: " + discounts.mkString(" ","%, ","% ")

}

trait VIPCustomer extends Customer {

  override def discounts = super.discounts ::: List(10)

}

trait GoldCustomer extends Customer {

override def discounts =

  if (discountCode.equals("H"))

     super.discounts ::: List(20)

  else super.discounts ::: List(15)

}

object Main {

def main(args: Array[String]) {

val myDiscounts = new Customer("Thomas","H") with VIPCustomer with GoldCustomer

println(myDiscounts)

}

}

scala> Main.main(Array.empty)

Applied discounts: 5%, 10%, 20%

Note that the order in which traits are stacked is important. They are calling each other from right to left.  GoldCustomer is, therefore, the first one to be called.

Static

companion objects

object Main {

def main(args: Array[String]) {

println("Hello Scala World !")

}

}

object Customer {

def apply()= new Customer("default name")

}

class Customer(name:String) {

}

exceptions

public class ConversionSample {

static Integer parse(String numberAsString) {

Integer number = null;

try {

number = Integer.parseInt(numberAsString);

} catch (NumberFormatExceptionnfe) {

System.err.println("Wrong format for "+numberAsString);

} catch (Exception ex) {

System.err.println("An unknown Error has occurred");

}

System.out.println("Parsed Number: "+number);

return number;

}

def parse(numberAsString: String) =

try {

Integer.parseInt(numberAsString)

} catch {

case nfe: NumberFormatException =>

println("Wrong format for number "+numberAsString)

case e: Exception => println("Error when parsing number"+

numberAsString)

}

返回值

case nfe: NumberFormatException =>

println("Wrong format for number "+numberAsString); -1

case _: Throwable =>

println("Error when parsing number "+numberAsString)

-1

scala.util.Left or  scala.util.Right type

case class Failure(val reason: String)

def parse(numberAsString: String) : Either[Failure,Int] =

try {

val result = Integer.parseInt(numberAsString)

Right(result)

} catch {

case _ : Throwable => Left(Failure("Error when parsing

number"))

}

 

String customerLevel = null;

if(amountBought > 3000) {

customerLevel = "Gold";

} else {

customerLevel = "Silver";

}

val amountBought = 5000

val customerLevel = if (amountBought> 3000) "Gold" else "Silver"

Scala的代碼風格

IDE

  http://docs.scala-lang.org/style/

Eclipse-based (including all the different versions of Eclipse, Typesafe's own bundled version known as Scala IDE as well as more commercial IDEs such as SpringSourceSTS), IntelliJ IDEA, and NetBeans.

SBT

Simple Build Tool (SBT) http://www.scala-sbt.org/

http://www.scala-sbt.org/0.13/tutorial/Hello.html 例子

http://www.scala-sbt.org/0.13/tutorial/Directories.html 目錄結構,Build.sbt是sbt的定義文件

SBT plugins

https://github.com/typesafehub/sbteclipse 基於sbt生成eclipse使用的工程文件

https://github.com/mpeltonen/sbt-idea 基於sbt生成IntelliJ使用的工程文件

https://github.com/dcaoyuan/nbscala/wiki/SbtIntegrationInNetBeans 基於sbt生成Netbeans使用的工程文件

  plugins.sbt中修改以生成

xsbt-web-plugin (available at  https://github.com/JamesEarlDouglas/xsbt-web-plugin ), a useful plugin to create traditional web apps that runs on a servlet container (such as Jetty).

v Plugins.sbt: addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.4.2")

Build.sbt:

  name := "SampleProject"

organization := "com.samples"

version := "1.0"

scalaVersion := "2.10.3"

seq(webSettings :_*)

libraryDependencies += "org.mortbay.jetty" % "jetty" % "6.1.22" % "container"

libraryDependencies += "javax.servlet" % "servlet-api" % "2.5" % "provided"

v sbt eclipse生成了eclipse工程

v 啟動web工程

> sbt

> container:start

v War包生成 > package

sbt-assembly

https://github.com/sbt/sbt-assembly

v Plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

v assembly.sbt

import AssemblyKeys._ // put this at the top of the file

assemblySettings

// your assembly settings here

Scalariform

plugins.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")

SBT中運行compile or  test:compile時,即按照scala風格格式化代碼

Scala Worksheets

Eclipse中建立的菜單

wpsA257.tmp

每次保存時,可以看到執行的結果

wpsA258.tmp

Java基礎庫上scala的封裝例子

http://dispatch.databinder.net/Dispatch.html HttpClient上的封裝

build.sbt添加libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.0"

import dispatch._, Defaults._

val request = url("http://freegeoip.net/xml/www.google.com")

val result = Http( request OK as.String)

val resultAsString = result()

def printer = new scala.xml.PrettyPrinter(90, 2)

for (xml <- citiesAsXML)

println(printer.format(xml))

for comprehension or  for expression

for (sequence) yield expression In the preceding code,  sequence can contain the following components:

•  Generators: They drive the iteration and are written in the following form:

element <- collection

•  Filters: They control the iteration and are written in the following form:

if expression

•  Definitions: They are local variable definitions and are written in the

following form:

variable = expression

for {

elem <- List(1,2,3,4,5)

} yield "T" + elem

for {

word <- List("Hello","Scala")

char <- word

} yield char.isLower

for {

word <- List("Hello","Scala")

char <- word if char.isUpper

} yield char

or {

word <- List("Hello","Scala")

char <- word

lowerChar = char.toLower

} yield lowerChar

測試

ScalaTest ( www.scalatest.org ) and Specs2 ( etorreborre.github.io/specs2/ ). ScalaCheck framework ( www.scalacheck.org )

Typesafe Activator包含必要的插件,項目根下執行> activator eclipse即生成eclipse工程

import org.scalatest.FunSuite

class Test01 extends FunSuite {

test("Very Basic") {

assert(1 == 1)

}

test("Another Very Basic") {

assert("Hello World" == "Hello World")

}

}

> activator

> test-only scalatest.Test01 (or scalatest.Test01.scala)

>~test-only scalatest.Test02  文件修改後自動運行單元測試【continuous mode】

The continuous mode works for the other SBT commands as well, such as  ~run or  ~test

作為Junit的單元測試方式運行

import org.junit.runner.RunWith

import org.scalatest.junit.JUnitRunner

import org.scalatest.FunSuite

@RunWith(classOf[JUnitRunner])

class MyTestSuite extends FunSuite ...

www.seleniumhq.org 功能測試

package scalatest

import org.scalatest._

import org.scalatest.selenium.WebBrowser

import org.openqa.selenium.htmlunit.HtmlUnitDriver

import org.openqa.selenium.firefox.FirefoxDriver

import org.openqa.selenium.WebDriver

class Test08 extends FlatSpec with Matchers with WebBrowser {

implicit val webDriver: WebDriver = new HtmlUnitDriver

go to "http://www.amazon.com"

click on "twotabsearchtextbox"

textField("twotabsearchtextbox").value = "Scala"

submit()

pageTitle should be ("Amazon.com: Scala")

pageSource should include("Scala Cookbook: Recipes")

}

www.scalamock.org

Web框架

alternatives to create web applications range from lightweight frameworks such as Unfiltered, Spray, or Scalatra to full-featured solutions such as the Lift or the Play Frameworks.

http://www.playframework.com/ http://www.playmodules.net

wpsA269.tmp

Web Service

Plugins.sbt

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" %"2.4.0")

addSbtPlugin("org.scalaxb" % "sbt-scalaxb" % "1.1.2")

resolvers += Resolver.sonatypeRepo("public")

build.sbt

import ScalaxbKeys._

name:="wssample"

scalaVersion:="2.10.2"

scalaxbSettings

ibraryDependencies += "net.databinder.dispatch" %% "dispatch-core" %"0.11.0"

libraryDependencies += "org.scalatest" %% "scalatest" % "2.0.M7" %"test"

sourceGenerators in Compile <+= scalaxb in Compile

packageName in scalaxb in Compile := "se.wssample"

XML/JSON

val books =

<Library>

<book title="Programming in Scala" quantity="15" price="30.00"

/>

<book title="Scala for Java Developers" quantity="10"

price="25.50" />

</Library>

import scala.xml._

val sameBooks = XML.loadString("""

<Library>

<book title="Programming in Scala" quantity="15"

price="30.00"/>

<book title="Scala for Java Developers" quantity="10"

price="25.50"/>

</Library>

""")

val total = (for {

book <- books \ "book"

price = ( book \ "@price").text.toDouble

quantity = ( book \ "@quantity").text.toInt

} yield price * quantity).sum

val books =

<Library>

{ List("Programming in Scala,15,30.00","Scala for Java

Developers,10,25.50") map { row => row split "," } map { b => <book

title={b(0)} quantity={b(1)} price={b(2)} /> }}

</Library>

import scala.util.parsing.json._

val result = JSON.parseFull("""

{

"Library": {

"book": [

{

"title": "Scala for Java Developers",

"quantity": 10

},

{

"title": "Programming Scala",

"quantity": 20

}

]}

}

""")

Futures and Promises

Scala Improvement Process (SIP)SIP-14-Futures and Promises

http://en.wikipedia.org/wiki/Futures_and_promises

•  async { <expression> } : In this construct,  <expression> is the code to

be executed asynchronously.

•  await { <expression returning a Future> } : This construct is included

in an  async block. It suspends the execution of the enclosing  async block

until the argument  Future is completed.

async future

Actor

Reactive Systems

http://www.reactivemanifesto.org/

反應式編程

Misc

MongoDB database, we are going to discover how the Casbah Scala toolkit https://github.com/mongodb/casbah

 

RPEL模式下拷貝部分代碼運行

scala> :paste

// Entering paste mode (ctrl-D to finish)

     

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 先決條件 1.1 已安裝JDK(版本1.5以上)並配置環境變數 到http://java.sun.com上下載JDK,配置環境變數(我的電腦右鍵->屬性->高級->環境變數) JAVA_HOME:JDK安裝目錄 CLASS_PATH:JDK安裝目錄\lib PATH:JDK安裝目錄\bin 1. ...
  • Hibernate4是一個開放源代碼的對象關係映射框架,它對JDBC進行了非常輕量級的對象封裝,使得Java程式員可以隨心所欲的使用對象編程思維來操縱資料庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程式使用, 也可以在Servlet/JSP的Web應用中使用,最 ...
  • 在程式出現問題時,我們需要找到並解決掉這些惱人的Bug,才能使程式順利的運行下去。但是,當代碼很多,程式很大的時候,找起來就很麻煩。 所以,我們需要藉助工具——Eclipse/MyEclipse中的Debug(調試)手段。而調試的時候,需要入口和觀測點,所以我們需要設置斷點來進行調試。 1 設置斷點 ...
  • MyEclipse 2016 CI 1有很多Web開發者會喜歡的新功能,包括Live Preview,帶有Map支持和hot-swap功能的JavaScript調試。另外還新增支持遠程WebSphere伺服器、Hibernate 5 和 CSS3。 | MyEclipse 2016 CI 1下載 L ...
  • 如果將所有的代碼都寫到一個文件中,當對於小項目來說不會有什麼問題,但是當它一個很大的工程的時候,如果將所有代碼都寫到一個文件中,不但開發起來很困難,維護更是頭疼,所以我們應該將代碼按不同的功能分別建立相應的代碼文件,下麵我們將program.c的代碼分成多個文件 首先,我們將功能代碼放到另一個文件c ...
  • 現在的目標是設計一個介面自動化測試框架 用例寫在excel裡面 利用python自帶的pyunit構建 之前已經安裝好了處理excel的模塊 這次簡單的使用下 提前創建好excel文件 “testcase.xls” 操作代碼如下: ...
  • 看起來不錯的一套一元雲購CMS源碼,源碼包裡面帶了安卓和ios手機客戶端,手機客戶端需要自己反編譯。 這裡不做功能和其它更多的介紹,可以自己下載後慢慢測試瞭解。 下麵演示圖為親測截圖<ignore_js_op> <ignore_js_op> <ignore_js_op> 源碼安裝說明:伺服器空間需要 ...
  • 2016年新版 恆信六合彩系統 帶新開獎結果》》》》》玩法齊全》連碼自由對碰》各類玩法內附說明。資料庫文件 hs001 修改資料庫連接 文件夾 configs 文件config後臺管理 admin admin前臺會員目錄 member <ignore_js_op> <ignore_js_op> <i ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...